<?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::create('marketing_requests', function (Blueprint $table) {
            $table->id();
            $table->string('request_number')->unique();
            $table->foreignId('request_type_id')->constrained('request_types')->onDelete('cascade');
            $table->foreignId('submitted_by')->constrained('users')->onDelete('cascade');
            $table->foreignId('assigned_designer_id')->nullable()->constrained('users')->onDelete('set null');
            $table->foreignId('agent_id')->nullable()->constrained('agents')->onDelete('set null');
            $table->string('property_url')->nullable();
            $table->string('property_address')->nullable();
            $table->text('instructions')->nullable();
            $table->dateTime('required_at');
            $table->string('priority')->default('standard'); // standard, priority, urgent
            $table->boolean('priority_override')->default(false);
            $table->text('urgent_reason')->nullable();
            $table->string('status')->default('NEW'); // NEW, IN_PROGRESS, AWAITING_ADMIN_REVIEW, CHANGES_REQUESTED, IN_REVISION, APPROVED, COMPLETED, CANCELLED
            $table->json('campaign_details')->nullable();
            
            // Workflow Timestamps & Users
            $table->dateTime('submitted_at');
            $table->dateTime('started_at')->nullable();
            $table->foreignId('started_by')->nullable()->constrained('users')->onDelete('set null');
            $table->dateTime('first_proof_at')->nullable();
            $table->dateTime('latest_proof_at')->nullable();
            $table->dateTime('approved_at')->nullable();
            $table->foreignId('approved_by')->nullable()->constrained('users')->onDelete('set null');
            $table->dateTime('completed_at')->nullable();
            $table->foreignId('completed_by')->nullable()->constrained('users')->onDelete('set null');
            $table->dateTime('cancelled_at')->nullable();
            
            $table->timestamps();
        });
    }

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