<?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('orders', function (Blueprint $table) {
            $table->id();
            $table->string('order_number')->unique();    // NSMA-2025-001
            $table->foreignId('client_id')->constrained('clients')->cascadeOnDelete();
            $table->foreignId('service_id')->nullable()->constrained('services')->nullOnDelete();
            $table->foreignId('assigned_to')->nullable()->constrained('users')->nullOnDelete();
            $table->string('title');
            $table->longText('description');
            $table->json('attachments')->nullable();      // file uploads
            $table->date('deadline')->nullable();
            $table->decimal('budget', 15, 2)->nullable();
            $table->enum('priority', ['low', 'normal', 'high', 'urgent'])->default('normal');
            $table->enum('status', [
                'draft',         // ноорог
                'submitted',     // илгээгдсэн
                'reviewing',     // судалж буй
                'approved',      // зөвшөөрсөн
                'in_progress',   // хийгдэж буй
                'on_hold',       // түр зогссон
                'completed',     // дууссан
                'cancelled',     // цуцлагдсан
                'rejected',      // татгалзсан
            ])->default('submitted');
            $table->text('admin_notes')->nullable();
            $table->timestamp('submitted_at')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['client_id', 'status']);
            $table->index(['status', 'created_at']);
        });

        Schema::create('order_messages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained('orders')->cascadeOnDelete();
            $table->morphs('sender');             // sender_type + sender_id (User эсвэл Client)
            $table->longText('message');
            $table->json('attachments')->nullable();
            $table->boolean('is_internal')->default(false);   // дотоод тэмдэглэл (харилцагч харахгүй)
            $table->timestamp('read_at')->nullable();
            $table->timestamps();

            $table->index('order_id');
        });

        Schema::create('order_status_history', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained('orders')->cascadeOnDelete();
            $table->foreignId('changed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('from_status')->nullable();
            $table->string('to_status');
            $table->text('comment')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('order_status_history');
        Schema::dropIfExists('order_messages');
        Schema::dropIfExists('orders');
    }
};
