<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            // Check and add columns only if they don't exist
            if (!Schema::hasColumn('orders', 'is_temp_bill')) {
                $table->boolean('is_temp_bill')->default(false)->after('order_number');
            }
            if (!Schema::hasColumn('orders', 'bill_type')) {
                $table->string('bill_type', 20)->default('temp')->after('status');
            }
            if (!Schema::hasColumn('orders', 'bill_number')) {
                $table->string('bill_number', 50)->nullable()->unique()->after('bill_type');
            }
            if (!Schema::hasColumn('orders', 'payment_received')) {
                $table->decimal('payment_received', 10, 2)->default(0.00)->after('total_amount');
            }
            if (!Schema::hasColumn('orders', 'change_amount')) {
                $table->decimal('change_amount', 10, 2)->default(0.00)->after('payment_received');
            }
            if (!Schema::hasColumn('orders', 'remaining_amount')) {
                $table->decimal('remaining_amount', 10, 2)->default(0.00)->after('change_amount');
            }
            if (!Schema::hasColumn('orders', 'finalized_at')) {
                $table->timestamp('finalized_at')->nullable()->after('completed_at');
            }
            if (!Schema::hasColumn('orders', 'locked_at')) {
                $table->timestamp('locked_at')->nullable()->after('finalized_at');
            }
            if (!Schema::hasColumn('orders', 'converted_to_final_at')) {
                $table->timestamp('converted_to_final_at')->nullable()->after('locked_at');
            }
            if (!Schema::hasColumn('orders', 'original_temp_bill_id')) {
                $table->unsignedBigInteger('original_temp_bill_id')->nullable()->after('converted_to_final_at');
            }
            if (!Schema::hasColumn('orders', 'print_count')) {
                $table->integer('print_count')->default(0)->after('original_temp_bill_id');
            }
        });

        // Add indexes and foreign keys after columns are created
        Schema::table('orders', function (Blueprint $table) {
            // Check if indexes exist before adding
            try {
                $table->foreign('original_temp_bill_id')->references('id')->on('orders')->onDelete('set null');
            } catch (\Exception $e) {
                // Foreign key might already exist
            }
        });

        // SQLite doesn't support ENUM, status is already a varchar column
        // No need to modify it
    }

    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            try {
                $table->dropForeign(['original_temp_bill_id']);
            } catch (\Exception $e) {
                // Foreign key might not exist
            }

            $table->dropColumn([
                'is_temp_bill',
                'bill_type',
                'bill_number',
                'payment_received',
                'change_amount',
                'remaining_amount',
                'finalized_at',
                'locked_at',
                'converted_to_final_at',
                'original_temp_bill_id',
                'print_count',
            ]);
        });
    }
};
