<?php

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

/**
 * Service reminders (spec §5.8 / §6.12): mileage/date/next-service reminders
 * per vehicle, dispatched by the `vrc:send-reminders` command via the ERP
 * notifier.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vrc_service_reminders')) {
            Schema::create('vrc_service_reminders', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('vehicle_id');
                $t->unsignedInteger('erp_customer_id');
                $t->string('type', 20)->default('date'); // mileage|date|next_service
                $t->date('due_date')->nullable();
                $t->unsignedInteger('due_odometer')->nullable();
                $t->unsignedBigInteger('last_service_id')->nullable();
                $t->string('status', 20)->default('pending'); // pending|sent|snoozed|done
                $t->string('channel', 20)->default('sms');
                $t->dateTime('scheduled_for')->nullable();
                $t->dateTime('sent_at')->nullable();
                $t->text('note')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'status']);
                $t->index(['business_id', 'vehicle_id'], 'vrc_reminder_biz_vehicle_idx');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('vrc_service_reminders');
    }
};
