<?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::table('bills', function (Blueprint $table) {
            if (!Schema::hasColumn('bills', 'dispatch_order_number')) {
                $table->string('dispatch_order_number', 64)->nullable()->after('tracking_code');
            }
            if (!Schema::hasColumn('bills', 'issue_time')) {
                $table->string('issue_time', 10)->nullable()->after('issue_date');
            }
            if (!Schema::hasColumn('bills', 'transport_start_date')) {
                $table->timestamp('transport_start_date')->nullable()->after('issue_time');
            }
            if (!Schema::hasColumn('bills', 'delivery_date')) {
                $table->timestamp('delivery_date')->nullable()->after('transport_start_date');
            }
            if (!Schema::hasColumn('bills', 'transport_type')) {
                $table->enum('transport_type', ['domestic', 'export', 'import', 'transit'])->default('domestic')->after('dispatch_order_number');
            }
            if (!Schema::hasColumn('bills', 'cargo_kind')) {
                $table->enum('cargo_kind', ['general', 'dangerous', 'perishable', 'oil'])->default('general')->after('transport_type');
            }
            if (!Schema::hasColumn('bills', 'service_type')) {
                $table->enum('service_type', ['retail', 'full_truck', 'combined'])->default('full_truck')->after('cargo_kind');
            }

            if (!Schema::hasColumn('bills', 'freight_company_id')) {
                $table->unsignedBigInteger('freight_company_id')->nullable()->after('load_id');
            }
            if (!Schema::hasColumn('bills', 'company_name')) {
                $table->string('company_name')->nullable()->after('freight_company_id');
            }
            if (!Schema::hasColumn('bills', 'company_national_id')) {
                $table->string('company_national_id', 20)->nullable()->after('company_name');
            }
            if (!Schema::hasColumn('bills', 'company_economic_code')) {
                $table->string('company_economic_code', 20)->nullable()->after('company_national_id');
            }
            if (!Schema::hasColumn('bills', 'company_phone')) {
                $table->string('company_phone', 20)->nullable()->after('company_economic_code');
            }
            if (!Schema::hasColumn('bills', 'company_ceo_name')) {
                $table->string('company_ceo_name')->nullable()->after('company_phone');
            }

            if (!Schema::hasColumn('bills', 'first_driver_name')) {
                $table->string('first_driver_name')->nullable()->after('company_ceo_name');
            }
            if (!Schema::hasColumn('bills', 'first_driver_national_code')) {
                $table->string('first_driver_national_code', 10)->nullable()->after('first_driver_name');
            }
            if (!Schema::hasColumn('bills', 'first_driver_mobile')) {
                $table->string('first_driver_mobile', 20)->nullable()->after('first_driver_national_code');
            }
            if (!Schema::hasColumn('bills', 'first_driver_account_number')) {
                $table->string('first_driver_account_number', 64)->nullable()->after('first_driver_mobile');
            }
            if (!Schema::hasColumn('bills', 'second_driver_name')) {
                $table->string('second_driver_name')->nullable()->after('first_driver_account_number');
            }
            if (!Schema::hasColumn('bills', 'second_driver_national_code')) {
                $table->string('second_driver_national_code', 10)->nullable()->after('second_driver_name');
            }
            if (!Schema::hasColumn('bills', 'second_driver_mobile')) {
                $table->string('second_driver_mobile', 20)->nullable()->after('second_driver_national_code');
            }

            if (!Schema::hasColumn('bills', 'vehicle_type')) {
                $table->string('vehicle_type', 64)->nullable()->after('second_driver_mobile');
            }
            if (!Schema::hasColumn('bills', 'vehicle_smart_card_no')) {
                $table->string('vehicle_smart_card_no', 64)->nullable()->after('vehicle_type');
            }
            if (!Schema::hasColumn('bills', 'vehicle_allowed_capacity')) {
                $table->decimal('vehicle_allowed_capacity', 12, 2)->nullable()->after('vehicle_smart_card_no');
            }
            if (!Schema::hasColumn('bills', 'vehicle_gross_weight_limit')) {
                $table->decimal('vehicle_gross_weight_limit', 12, 2)->nullable()->after('vehicle_allowed_capacity');
            }
            if (!Schema::hasColumn('bills', 'vehicle_owner_national_code')) {
                $table->string('vehicle_owner_national_code', 10)->nullable()->after('vehicle_gross_weight_limit');
            }

            if (!Schema::hasColumn('bills', 'origin_postal_code')) {
                $table->string('origin_postal_code', 10)->nullable()->after('sender_info');
            }
            if (!Schema::hasColumn('bills', 'destination_postal_code')) {
                $table->string('destination_postal_code', 10)->nullable()->after('receiver_info');
            }
            if (!Schema::hasColumn('bills', 'distance_km')) {
                $table->decimal('distance_km', 10, 2)->nullable()->after('destination_postal_code');
            }

            if (!Schema::hasColumn('bills', 'sender_name')) {
                $table->string('sender_name')->nullable()->after('distance_km');
            }
            if (!Schema::hasColumn('bills', 'sender_national_id')) {
                $table->string('sender_national_id', 20)->nullable()->after('sender_name');
            }
            if (!Schema::hasColumn('bills', 'sender_phone')) {
                $table->string('sender_phone', 20)->nullable()->after('sender_national_id');
            }
            if (!Schema::hasColumn('bills', 'sender_address')) {
                $table->text('sender_address')->nullable()->after('sender_phone');
            }
            if (!Schema::hasColumn('bills', 'sender_postal_code')) {
                $table->string('sender_postal_code', 10)->nullable()->after('sender_address');
            }
            if (!Schema::hasColumn('bills', 'receiver_name_full')) {
                $table->string('receiver_name_full')->nullable()->after('sender_postal_code');
            }
            if (!Schema::hasColumn('bills', 'receiver_national_id')) {
                $table->string('receiver_national_id', 20)->nullable()->after('receiver_name_full');
            }
            if (!Schema::hasColumn('bills', 'receiver_mobile')) {
                $table->string('receiver_mobile', 20)->nullable()->after('receiver_national_id');
            }
            if (!Schema::hasColumn('bills', 'receiver_address_full')) {
                $table->text('receiver_address_full')->nullable()->after('receiver_mobile');
            }
            if (!Schema::hasColumn('bills', 'receiver_postal_code')) {
                $table->string('receiver_postal_code', 10)->nullable()->after('receiver_address_full');
            }

            if (!Schema::hasColumn('bills', 'freight_amount')) {
                $table->decimal('freight_amount', 14, 2)->nullable()->after('declared_value');
            }
            if (!Schema::hasColumn('bills', 'insurance_amount')) {
                $table->decimal('insurance_amount', 14, 2)->nullable()->after('freight_amount');
            }
            if (!Schema::hasColumn('bills', 'tax_amount')) {
                $table->decimal('tax_amount', 14, 2)->nullable()->after('insurance_amount');
            }
            if (!Schema::hasColumn('bills', 'toll_amount')) {
                $table->decimal('toll_amount', 14, 2)->nullable()->after('tax_amount');
            }
            if (!Schema::hasColumn('bills', 'total_amount')) {
                $table->decimal('total_amount', 14, 2)->nullable()->after('toll_amount');
            }

            if (!Schema::hasColumn('bills', 'transport_notes')) {
                $table->text('transport_notes')->nullable()->after('notes');
            }
            if (!Schema::hasColumn('bills', 'contract_terms')) {
                $table->text('contract_terms')->nullable()->after('transport_notes');
            }
            if (!Schema::hasColumn('bills', 'payment_method')) {
                $table->string('payment_method', 64)->nullable()->after('contract_terms');
            }

            if (!Schema::hasColumn('bills', 'driver_signature')) {
                $table->text('driver_signature')->nullable()->after('digital_signature');
            }
            if (!Schema::hasColumn('bills', 'organization_approval')) {
                $table->boolean('organization_approval')->default(false)->after('driver_signature');
            }
            if (!Schema::hasColumn('bills', 'organization_confirmation_code')) {
                $table->string('organization_confirmation_code', 64)->nullable()->after('organization_approval');
            }

            if (!Schema::hasColumn('bills', 'container_no')) {
                $table->string('container_no', 64)->nullable()->after('organization_confirmation_code');
            }
            if (!Schema::hasColumn('bills', 'weighbridge_code')) {
                $table->string('weighbridge_code', 64)->nullable()->after('container_no');
            }
            if (!Schema::hasColumn('bills', 'weighbridge_receipt_no')) {
                $table->string('weighbridge_receipt_no', 64)->nullable()->after('weighbridge_code');
            }
            if (!Schema::hasColumn('bills', 'order_type')) {
                $table->string('order_type', 64)->nullable()->after('weighbridge_receipt_no');
            }

            if (!Schema::hasColumn('bills', 'state')) {
                $table->enum('state', ['draft', 'dispatch_registered', 'issued', 'in_transit', 'delivered', 'cancelled'])->default('draft')->after('order_type');
            }
        });

        if (!Schema::hasTable('bill_cargo_items')) {
            Schema::create('bill_cargo_items', function (Blueprint $table) {
                $table->id();
                $table->foreignId('bill_id')->constrained('bills')->onDelete('cascade');
                $table->string('cargo_name');
                $table->string('cargo_code', 64);
                $table->string('packaging_type', 64)->nullable();
                $table->integer('quantity')->default(1);
                $table->decimal('weight', 12, 2);
                $table->decimal('value', 14, 2);
                $table->enum('oil_type', ['oil', 'non_oil'])->default('non_oil');
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('bill_cargo_items')) {
            Schema::drop('bill_cargo_items');
        }
    }
};
