<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('clients', function (Blueprint $t) {
            $t->string('telegram_chat_id', 32)->nullable()->after('telegram');
            $t->json('notify_prefs')->nullable()->after('telegram_chat_id');
            $t->index(['company_id', 'telegram_chat_id']);
        });

        Schema::create('service_reminders_sent', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('vehicle_id')->constrained()->cascadeOnDelete();
            $t->foreignId('client_id')->nullable()->constrained()->nullOnDelete();
            $t->string('channel', 16);    // email / telegram
            $t->string('type', 24)->default('general'); // general / inspection / oil / etc
            $t->dateTime('sent_at');
            $t->timestamps();

            $t->index(['company_id', 'vehicle_id', 'sent_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('service_reminders_sent');
        Schema::table('clients', function (Blueprint $t) {
            $t->dropIndex(['company_id', 'telegram_chat_id']);
            $t->dropColumn(['telegram_chat_id', 'notify_prefs']);
        });
    }
};
