<?php

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

/**
 * Generated documents (contract / BEO / proposal / run-sheet / visit summary)
 * for a booking or lead. The PDF is stored on a private disk and streamed
 * through an authenticated endpoint; contract acceptance is stamped here.
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('bms_documents')) {
            return;
        }
        Schema::create('bms_documents', function (Blueprint $t) {
            $t->bigIncrements('id');
            $t->unsignedInteger('business_id');
            $t->unsignedBigInteger('booking_id')->nullable();
            $t->unsignedBigInteger('lead_id')->nullable();
            $t->string('type', 30); // contract|beo|proposal|run_sheet|visit_summary
            $t->string('disk', 30)->default('local');
            $t->string('path', 255);
            $t->unsignedInteger('version')->default(1);
            $t->json('meta')->nullable();
            $t->unsignedInteger('generated_by')->nullable();
            $t->dateTime('accepted_at')->nullable();
            $t->string('accepted_ip', 64)->nullable();
            $t->timestamps();
            $t->index(['business_id', 'booking_id']);
            $t->index(['business_id', 'type']);
        });
    }

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