<?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
    {
        if (! Schema::hasTable('passenger_extra_service_items')) {
            Schema::create('passenger_extra_service_items', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('passenger_extra_service_id');
                $table->string('title');
                $table->string('subtitle')->nullable();
                $table->string('image_path', 512)->nullable();
                $table->decimal('price', 12, 2)->default(0);
                $table->decimal('discount_percent', 5, 2)->default(0);
                $table->unsignedInteger('sort_order')->default(0);
                $table->boolean('is_active')->default(true);
                $table->timestamps();

                $table->foreign('passenger_extra_service_id', 'fk_pexitem_service')
                    ->references('id')
                    ->on('passenger_extra_services')
                    ->cascadeOnDelete();
            });
        }

        if (Schema::hasTable('passenger_extra_service_requests')) {
            Schema::table('passenger_extra_service_requests', function (Blueprint $table) {
                if (! Schema::hasColumn('passenger_extra_service_requests', 'items_snapshot')) {
                    $table->json('items_snapshot')->nullable()->after('notes');
                }
                if (! Schema::hasColumn('passenger_extra_service_requests', 'subtotal_amount')) {
                    $table->decimal('subtotal_amount', 12, 2)->default(0)->after('fee_snapshot');
                }
                if (! Schema::hasColumn('passenger_extra_service_requests', 'app_fee_amount')) {
                    $table->decimal('app_fee_amount', 12, 2)->default(0)->after('subtotal_amount');
                }
                if (! Schema::hasColumn('passenger_extra_service_requests', 'delivery_fee_amount')) {
                    $table->decimal('delivery_fee_amount', 12, 2)->default(0)->after('app_fee_amount');
                }
                if (! Schema::hasColumn('passenger_extra_service_requests', 'total_amount')) {
                    $table->decimal('total_amount', 12, 2)->default(0)->after('delivery_fee_amount');
                }
                if (! Schema::hasColumn('passenger_extra_service_requests', 'contact_phone')) {
                    $table->string('contact_phone', 32)->nullable()->after('admin_notes');
                }
            });
        }

        if (Schema::hasTable('app_settings')) {
            Schema::table('app_settings', function (Blueprint $table) {
                if (! Schema::hasColumn('app_settings', 'passenger_extra_service_delivery_fee')) {
                    $table->decimal('passenger_extra_service_delivery_fee', 12, 2)->default(0);
                }
                if (! Schema::hasColumn('app_settings', 'passenger_extra_service_app_fee')) {
                    $table->decimal('passenger_extra_service_app_fee', 12, 2)->default(600);
                }
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('app_settings')) {
            Schema::table('app_settings', function (Blueprint $table) {
                foreach (['passenger_extra_service_delivery_fee', 'passenger_extra_service_app_fee'] as $col) {
                    if (Schema::hasColumn('app_settings', $col)) {
                        $table->dropColumn($col);
                    }
                }
            });
        }

        if (Schema::hasTable('passenger_extra_service_requests')) {
            Schema::table('passenger_extra_service_requests', function (Blueprint $table) {
                foreach ([
                    'items_snapshot',
                    'subtotal_amount',
                    'app_fee_amount',
                    'delivery_fee_amount',
                    'total_amount',
                    'contact_phone',
                ] as $col) {
                    if (Schema::hasColumn('passenger_extra_service_requests', $col)) {
                        $table->dropColumn($col);
                    }
                }
            });
        }

        Schema::dropIfExists('passenger_extra_service_items');
    }
};

