<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('projects', function (Blueprint $table) {
            $table->unsignedInteger('code')->nullable()->after('id');
            $table->boolean('sale_registration_enabled')->default(true)->after('status');
            $table->dateTime('sale_deactivation_at')->nullable()->after('sale_registration_enabled');
            $table->foreignId('product_id')->nullable()->after('category_id')->constrained('products')->nullOnDelete();
            $table->foreignId('breed_id')->nullable()->after('product_id')->constrained('breeds')->nullOnDelete();
            $table->unsignedInteger('quantity')->nullable()->after('breed_id');
            $table->string('unit', 50)->nullable()->after('quantity'); // head/count/weight: راس، تعداد، وزن
            $table->decimal('unit_price', 15, 0)->nullable()->after('unit');
            $table->json('images')->nullable()->after('unit_price');
            $table->date('presale_start')->nullable()->after('deadline');
            $table->date('presale_end')->nullable()->after('presale_start');
            $table->decimal('estimated_profit', 15, 0)->nullable()->after('budget');
            $table->text('contract_text')->nullable()->after('notes');
        });

        // Backfill code for existing rows (sequential), then add unique index
        $ids = DB::table('projects')->orderBy('id')->pluck('id');
        $code = 1;
        foreach ($ids as $id) {
            DB::table('projects')->where('id', $id)->update(['code' => $code++]);
        }
        Schema::table('projects', function (Blueprint $table) {
            $table->unique('code');
        });

        // Extend status enum for admin workflow (تایید/رد/ویرایش/انتظار/داوری/امضا نشده)
        $driver = Schema::getConnection()->getDriverName();
        if ($driver === 'mysql') {
            DB::statement("ALTER TABLE projects MODIFY COLUMN status ENUM(
                'pending','active','completed','cancelled','expired',
                'approved','rejected','edit','waiting','arbitration','unsigned'
            ) DEFAULT 'pending'");
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('projects', function (Blueprint $table) {
            $table->dropUnique(['code']);
            $table->dropForeign(['product_id']);
            $table->dropForeign(['breed_id']);
            $table->dropColumn([
                'code',
                'sale_registration_enabled',
                'sale_deactivation_at',
                'product_id',
                'breed_id',
                'quantity',
                'unit',
                'unit_price',
                'images',
                'presale_start',
                'presale_end',
                'estimated_profit',
                'contract_text',
            ]);
        });

        $driver = Schema::getConnection()->getDriverName();
        if ($driver === 'mysql') {
            DB::statement("ALTER TABLE projects MODIFY COLUMN status ENUM('pending','active','completed','cancelled','expired') DEFAULT 'pending'");
        }
    }
};
