<?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
    {
        // Service bays / lifts.
        Schema::create('posts', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->string('name');                  // "Pod 1", "Pod 2"
            $t->string('color', 16)->default('#3B82F6');
            $t->boolean('is_active')->default(true);
            $t->unsignedSmallInteger('sort_order')->default(0);
            $t->timestamps();

            $t->index(['company_id', 'is_active', 'sort_order']);
        });

        Schema::create('appointments', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('post_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('client_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('vehicle_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('master_id')->nullable()->constrained('users')->nullOnDelete();
            $t->foreignId('deal_id')->nullable()->constrained()->nullOnDelete();

            $t->date('date');
            $t->time('time_start');
            $t->time('time_end');
            $t->string('title');
            $t->string('color', 16)->nullable();
            $t->string('status')->default('scheduled'); // scheduled/arrived/done/cancelled/no_show
            $t->text('notes')->nullable();

            $t->timestamps();

            $t->index(['company_id', 'date']);
            $t->index(['company_id', 'post_id', 'date']);
            $t->index(['company_id', 'master_id', 'date']);
        });
    }

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