<?php

$dir = __DIR__ . '/database/migrations';
$files = scandir($dir);

$schemas = [
    'bookings' => <<<EOT
            \$table->id();
            \$table->string('booking_no')->unique();
            \$table->date('booking_date');
            
            // Customer Info
            \$table->string('customer_name');
            \$table->string('mobile');
            \$table->string('email')->nullable();
            \$table->string('company_name')->nullable();
            \$table->string('gst_number')->nullable();
            \$table->text('address')->nullable();
            \$table->string('reference')->nullable();
            
            // Job Information
            \$table->string('job_name')->nullable();
            \$table->text('job_description')->nullable();
            \$table->text('special_instructions')->nullable();
            \$table->date('expected_delivery_date')->nullable();
            \$table->time('delivery_time')->nullable();
            \$table->unsignedBigInteger('job_priority_id')->nullable();
            
            // Cost Details
            \$table->decimal('design_charge', 10, 2)->default(0);
            \$table->decimal('printing_charge', 10, 2)->default(0);
            \$table->decimal('paper_charge', 10, 2)->default(0);
            \$table->decimal('plate_charge', 10, 2)->default(0);
            \$table->decimal('lamination_charge', 10, 2)->default(0);
            \$table->decimal('binding_charge', 10, 2)->default(0);
            \$table->decimal('cutting_charge', 10, 2)->default(0);
            \$table->decimal('packing_charge', 10, 2)->default(0);
            \$table->decimal('transport_charge', 10, 2)->default(0);
            \$table->decimal('installation_charge', 10, 2)->default(0);
            \$table->decimal('other_charge', 10, 2)->default(0);
            \$table->decimal('discount', 10, 2)->default(0);
            \$table->decimal('gst_percentage', 5, 2)->default(0);
            \$table->decimal('round_off', 5, 2)->default(0);
            \$table->decimal('grand_total', 12, 2)->default(0);
            
            \$table->string('status')->default('Pending');
            \$table->unsignedBigInteger('created_by')->nullable();
            \$table->unsignedBigInteger('updated_by')->nullable();
            \$table->timestamps();
EOT
,
    'booking_items' => <<<EOT
            \$table->id();
            \$table->foreignId('booking_id')->constrained('bookings')->onDelete('cascade');
            \$table->unsignedBigInteger('category_id')->nullable();
            \$table->unsignedBigInteger('paper_size_id')->nullable();
            \$table->decimal('custom_width', 8, 2)->nullable();
            \$table->decimal('custom_height', 8, 2)->nullable();
            \$table->string('custom_unit')->nullable();
            \$table->unsignedBigInteger('paper_type_id')->nullable();
            \$table->unsignedBigInteger('paper_brand_id')->nullable();
            \$table->unsignedBigInteger('paper_gsm_id')->nullable();
            \$table->unsignedBigInteger('print_type_id')->nullable();
            \$table->unsignedBigInteger('color_type_id')->nullable();
            \$table->unsignedBigInteger('print_method_id')->nullable();
            \$table->unsignedBigInteger('lamination_id')->nullable();
            \$table->unsignedBigInteger('lamination_side_id')->nullable();
            \$table->unsignedBigInteger('corner_type_id')->nullable();
            \$table->unsignedBigInteger('binding_id')->nullable();
            \$table->unsignedBigInteger('folding_id')->nullable();
            
            \$table->integer('quantity')->default(1);
            \$table->integer('extra_copies')->default(0);
            \$table->decimal('waste_percentage', 5, 2)->default(0);
            \$table->integer('total_quantity')->default(1);
            \$table->timestamps();
EOT
,
    'booking_other_costs' => <<<EOT
            \$table->id();
            \$table->foreignId('booking_id')->constrained('bookings')->onDelete('cascade');
            \$table->string('description');
            \$table->decimal('amount', 10, 2)->default(0);
            \$table->timestamps();
EOT
,
    'booking_status_histories' => <<<EOT
            \$table->id();
            \$table->foreignId('booking_id')->constrained('bookings')->onDelete('cascade');
            \$table->unsignedBigInteger('job_status_id')->nullable();
            \$table->string('status_name')->nullable();
            \$table->text('notes')->nullable();
            \$table->unsignedBigInteger('changed_by')->nullable();
            \$table->timestamps();
EOT
,
    'booking_payments' => <<<EOT
            \$table->id();
            \$table->foreignId('booking_id')->constrained('bookings')->onDelete('cascade');
            \$table->unsignedBigInteger('payment_term_id')->nullable();
            \$table->decimal('advance_amount', 12, 2)->default(0);
            \$table->decimal('balance_amount', 12, 2)->default(0);
            \$table->timestamps();
EOT
,
    'booking_deliveries' => <<<EOT
            \$table->id();
            \$table->foreignId('booking_id')->constrained('bookings')->onDelete('cascade');
            \$table->unsignedBigInteger('delivery_type_id')->nullable();
            \$table->text('delivery_address')->nullable();
            \$table->string('receiver_name')->nullable();
            \$table->string('receiver_mobile')->nullable();
            \$table->timestamps();
EOT
,
    'booking_notes' => <<<EOT
            \$table->id();
            \$table->foreignId('booking_id')->constrained('bookings')->onDelete('cascade');
            \$table->text('note');
            \$table->unsignedBigInteger('created_by')->nullable();
            \$table->timestamps();
EOT
];

foreach ($files as $file) {
    if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
        foreach ($schemas as $table => $schema) {
            if (strpos($file, 'create_' . $table . '_table') !== false) {
                $path = $dir . '/' . $file;
                $content = file_get_contents($path);
                
                $pattern = '/Schema::create\(\'' . $table . '\', function \(Blueprint \$table\) \{(.*?)\}\);/s';
                $replacement = "Schema::create('$table', function (Blueprint \$table) {\n$schema\n        });";
                
                $newContent = preg_replace($pattern, $replacement, $content);
                file_put_contents($path, $newContent);
                echo "Updated: $file\n";
                break;
            }
        }
    }
}
echo "Done.\n";
