<?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('users', function (Blueprint $table) {
            if (!Schema::hasColumn('users', 'smart_card_number')) {
                $table->string('smart_card_number', 32)->nullable()->after('vehicle_plate');
            }
            if (!Schema::hasColumn('users', 'license_number')) {
                $table->string('license_number', 32)->nullable()->after('smart_card_number');
            }
            if (!Schema::hasColumn('users', 'vehicle_capacity')) {
                $table->decimal('vehicle_capacity', 10, 2)->nullable()->after('vehicle_type');
            }
        });

        Schema::table('loads', function (Blueprint $table) {
            $table->string('sender_name')->nullable()->after('title');
            $table->string('sender_national_code', 10)->nullable()->after('sender_name');
            $table->string('sender_phone', 16)->nullable()->after('sender_national_code');
            $table->text('sender_address')->nullable()->after('sender_phone');

            $table->string('receiver_name')->nullable()->after('sender_address');
            $table->string('receiver_national_code', 10)->nullable()->after('receiver_name');
            $table->string('receiver_phone', 16)->nullable()->after('receiver_national_code');
            $table->text('receiver_address')->nullable()->after('receiver_phone');

            $table->string('driver_smart_card_number', 32)->nullable()->after('driver_id');
            $table->string('driver_license_number', 32)->nullable()->after('driver_smart_card_number');

            $table->string('fleet_plate_number', 32)->nullable()->after('destination_lng');
            $table->string('fleet_type', 64)->nullable()->after('fleet_plate_number');
            $table->decimal('fleet_capacity', 10, 2)->nullable()->after('fleet_type');
            $table->string('route_type_code', 64)->nullable()->after('fleet_capacity');
            $table->boolean('is_night_trip')->default(false)->after('route_type_code');
            $table->boolean('is_holiday_trip')->default(false)->after('is_night_trip');

            $table->string('origin_city', 128)->nullable()->after('origin_address');
            $table->string('destination_city', 128)->nullable()->after('destination_address');

            $table->string('cargo_type', 128)->nullable()->after('load_type');
            $table->string('cargo_standard_code', 64)->nullable()->after('cargo_type');
            $table->string('cargo_packaging', 64)->nullable()->after('cargo_standard_code');
            $table->decimal('cargo_value', 14, 2)->nullable()->after('price');

            $table->boolean('is_hazardous')->default(false)->after('cargo_packaging');
            $table->boolean('requires_permit')->default(false)->after('is_hazardous');
            $table->boolean('requires_second_driver')->default(false)->after('requires_permit');
            $table->decimal('gvw', 10, 2)->nullable()->after('requires_second_driver');
            $table->decimal('gcw', 10, 2)->nullable()->after('gvw');

            $table->decimal('distance_km', 10, 2)->nullable()->after('gcw');
            $table->decimal('freight_amount', 14, 2)->nullable()->after('distance_km');
            $table->decimal('insurance_amount', 14, 2)->nullable()->after('freight_amount');
            $table->decimal('legal_toll_amount', 14, 2)->nullable()->after('insurance_amount');
            $table->decimal('tax_amount', 14, 2)->nullable()->after('legal_toll_amount');
            $table->decimal('terminal_share_amount', 14, 2)->nullable()->after('tax_amount');
            $table->boolean('minimum_fare_applied')->default(false)->after('legal_toll_amount');
            $table->decimal('final_price', 14, 2)->nullable()->after('minimum_fare_applied');
            $table->text('payment_terms')->nullable()->after('final_price');

            $table->string('dispatch_order_number', 64)->nullable()->after('payment_terms');
            $table->timestamp('dispatch_issued_at')->nullable()->after('dispatch_order_number');
            $table->timestamp('admin_approved_at')->nullable()->after('dispatch_issued_at');
            $table->timestamp('driver_approved_at')->nullable()->after('admin_approved_at');

            $table->unsignedBigInteger('contract_id')->nullable()->after('driver_approved_at');
        });

        Schema::table('bills', function (Blueprint $table) {
            $table->string('internal_serial', 64)->nullable()->unique()->after('bill_number');
            $table->string('tracking_code', 64)->nullable()->after('internal_serial');
            $table->string('qr_code_content')->nullable()->after('tracking_code');
            $table->timestamp('cancelled_at')->nullable()->after('delivery_confirmation_at');
            $table->text('cancel_reason')->nullable()->after('cancelled_at');
            $table->timestamp('driver_receipt_at')->nullable()->after('cancel_reason');
            $table->text('driver_receipt_notes')->nullable()->after('driver_receipt_at');
            $table->text('payment_terms')->nullable()->after('driver_receipt_notes');
            $table->boolean('is_returned')->default(false)->after('payment_terms');
            $table->timestamp('returned_at')->nullable()->after('is_returned');
            $table->string('digital_signature_provider', 64)->default('fake')->after('returned_at');
            $table->text('digital_signature')->nullable()->after('digital_signature_provider');
        });

        Schema::create('contracts', function (Blueprint $table) {
            $table->id();
            $table->string('contract_number')->unique();
            $table->string('title');
            $table->string('company_name')->nullable();
            $table->date('start_date')->nullable();
            $table->date('end_date')->nullable();
            $table->decimal('base_rate_per_km', 14, 2)->nullable();
            $table->decimal('minimum_rate', 14, 2)->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('activity_logs', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->nullable();
            $table->string('method', 10);
            $table->string('path');
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->json('payload')->nullable();
            $table->integer('response_status')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['user_id', 'created_at']);
            $table->index(['path']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('activity_logs');
        Schema::dropIfExists('contracts');

        Schema::table('bills', function (Blueprint $table) {
            $table->dropColumn([
                'internal_serial',
                'tracking_code',
                'qr_code_content',
                'cancelled_at',
                'cancel_reason',
                'driver_receipt_at',
                'driver_receipt_notes',
                'payment_terms',
                'is_returned',
                'returned_at',
                'digital_signature_provider',
                'digital_signature',
            ]);
        });

        Schema::table('loads', function (Blueprint $table) {
            $table->dropColumn([
                'sender_name',
                'sender_national_code',
                'sender_phone',
                'sender_address',
                'receiver_name',
                'receiver_national_code',
                'receiver_phone',
                'receiver_address',
                'driver_smart_card_number',
                'driver_license_number',
                'fleet_plate_number',
                'fleet_type',
                'fleet_capacity',
                'route_type_code',
                'is_night_trip',
                'is_holiday_trip',
                'origin_city',
                'destination_city',
                'cargo_type',
                'cargo_standard_code',
                'cargo_packaging',
                'cargo_value',
                'is_hazardous',
                'requires_permit',
                'requires_second_driver',
                'gvw',
                'gcw',
                'distance_km',
                'freight_amount',
                'insurance_amount',
                'legal_toll_amount',
                'tax_amount',
                'terminal_share_amount',
                'minimum_fare_applied',
                'final_price',
                'payment_terms',
                'dispatch_order_number',
                'dispatch_issued_at',
                'admin_approved_at',
                'driver_approved_at',
                'contract_id',
            ]);
        });

        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn([
                'smart_card_number',
                'license_number',
                'vehicle_capacity',
            ]);
        });
    }
};
