<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Custom Orders table
        Schema::create('custom_orders', function (Blueprint $table) {
            $table->id();
            $table->uuid()->unique()->index();
            $table->foreignId('user_id')->nullable()->constrained()->onDelete('set null');
            $table->string('order_number')->unique();
            $table->enum('type', ['wallpaper', 'mural', 'fabric']);
            $table->enum('status', ['submitted', 'approved', 'rejected', 'in_production', 'proof_ready', 'completed'])->default('submitted');
            $table->decimal('design_fee', 10, 2);
            $table->boolean('library_discount_applied')->default(false);
            $table->decimal('material_cost', 10, 2)->default(0);
            $table->decimal('total_cost', 10, 2);
            $table->decimal('deposit_amount', 10, 2);
            $table->decimal('balance_amount', 10, 2);
            $table->enum('deposit_status', ['pending', 'paid', 'failed'])->default('pending');
            $table->enum('balance_status', ['pending', 'paid', 'failed'])->default('pending');
            $table->text('customer_brief');
            $table->text('admin_notes')->nullable();
            $table->timestamp('submitted_at')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('rejected_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamps();
        });

        // Custom Order Specifications table
        Schema::create('custom_order_specifications', function (Blueprint $table) {
            $table->id();
            $table->foreignId('custom_order_id')->constrained('custom_orders')->onDelete('cascade');
            $table->decimal('length', 10, 2)->nullable(); // wallpaper length in meters
            $table->decimal('width', 10, 2)->nullable(); // mural/wallpaper width in meters
            $table->decimal('height', 10, 2)->nullable(); // mural height in meters
            $table->foreignId('print_stock_id')->nullable()->constrained('print_stocks')->onDelete('set null');
            $table->integer('quantity')->default(1);
            $table->text('special_instructions')->nullable();
            $table->timestamps();
        });

        // Custom Order Files table (reference images, design files)
        Schema::create('custom_order_files', function (Blueprint $table) {
            $table->id();
            $table->foreignId('custom_order_id')->constrained('custom_orders')->onDelete('cascade');
            $table->enum('file_type', ['reference_image', 'design_file', 'specification']);
            $table->string('file_path');
            $table->string('original_filename');
            $table->integer('file_size');
            $table->string('mime_type');
            $table->foreignId('uploaded_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
        });

        // Custom Order Proofs table (admin-uploaded proofs)
        Schema::create('custom_order_proofs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('custom_order_id')->constrained('custom_orders')->onDelete('cascade');
            $table->string('file_path');
            $table->string('original_filename');
            $table->integer('file_size');
            $table->string('mime_type');
            $table->text('notes')->nullable();
            $table->foreignId('uploaded_by')->constrained('users')->onDelete('restrict');
            $table->enum('status', ['pending', 'approved', 'rejected'])->default('pending');
            $table->text('rejection_reason')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('rejected_at')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('custom_order_proofs');
        Schema::dropIfExists('custom_order_files');
        Schema::dropIfExists('custom_order_specifications');
        Schema::dropIfExists('custom_orders');
    }
};
