<?php

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

return new class extends Migration {
    /**
     * Fiyatlandırma modülünü kaldırıp fiyatı tamamen tarihlere taşıyoruz.
     * Kültür + Yurt Dışı tarih tablolarına oda bazlı yeni fiyat alanları eklenir,
     * eski pricing FK'si ve tek çocuk fiyatı kaldırılır (veri child_price_1'e taşınır).
     */
    public function up(): void
    {
        // ── Kültür Turları (cultural_tour_dates) ─────────────────────────────
        Schema::table('cultural_tour_dates', function (Blueprint $table) {
            $table->decimal('single_supplement', 10, 2)->nullable()->after('adult_price');
            $table->decimal('extra_bed_price', 10, 2)->nullable()->after('single_supplement');
            $table->decimal('child_price_1', 10, 2)->nullable()->after('extra_bed_price');
            $table->decimal('child_price_2', 10, 2)->nullable()->after('child_price_1');
            $table->integer('room_capacity')->nullable()->after('infant_capacity');
            $table->integer('infant_min_age')->nullable()->after('child_max_age');
            $table->boolean('use_custom_payment_plan')->default(false)->after('payment_schedule');
        });

        // Mevcut tek çocuk fiyatını 1. çocuk fiyatına taşı
        DB::statement('UPDATE cultural_tour_dates SET child_price_1 = child_price');

        Schema::table('cultural_tour_dates', function (Blueprint $table) {
            $table->dropForeign(['cultural_tour_pricing_id']);
            $table->dropIndex(['cultural_tour_pricing_id']);
            $table->dropColumn(['cultural_tour_pricing_id', 'child_price']);
        });

        // ── Yurt Dışı Turları (international_tour_dates) ──────────────────────
        Schema::table('international_tour_dates', function (Blueprint $table) {
            $table->decimal('single_supplement', 10, 2)->nullable()->after('adult_price');
            $table->decimal('extra_bed_price', 10, 2)->nullable()->after('single_supplement');
            $table->decimal('child_price_1', 10, 2)->nullable()->after('extra_bed_price');
            $table->decimal('child_price_2', 10, 2)->nullable()->after('child_price_1');
            $table->integer('room_capacity')->nullable()->after('infant_capacity');
            $table->integer('infant_min_age')->nullable()->after('child_max_age');
            $table->boolean('use_custom_payment_plan')->default(false)->after('payment_schedule');
        });

        DB::statement('UPDATE international_tour_dates SET child_price_1 = child_price');

        Schema::table('international_tour_dates', function (Blueprint $table) {
            $table->dropForeign(['international_tour_pricing_id']);
            $table->dropIndex('idx_itd_pricing');
            $table->dropColumn(['international_tour_pricing_id', 'child_price']);
        });
    }

    public function down(): void
    {
        // ── Kültür Turları ───────────────────────────────────────────────────
        Schema::table('cultural_tour_dates', function (Blueprint $table) {
            $table->unsignedBigInteger('cultural_tour_pricing_id')->nullable()->after('cultural_tour_id');
            $table->decimal('child_price', 10, 2)->nullable()->after('adult_price');
            $table->index(['cultural_tour_pricing_id']);
        });

        DB::statement('UPDATE cultural_tour_dates SET child_price = child_price_1');

        Schema::table('cultural_tour_dates', function (Blueprint $table) {
            $table->dropColumn([
                'single_supplement', 'extra_bed_price', 'child_price_1', 'child_price_2',
                'room_capacity', 'infant_min_age', 'use_custom_payment_plan',
            ]);
        });

        // ── Yurt Dışı Turları ────────────────────────────────────────────────
        Schema::table('international_tour_dates', function (Blueprint $table) {
            $table->unsignedBigInteger('international_tour_pricing_id')->nullable()->after('international_tour_id');
            $table->decimal('child_price', 10, 2)->nullable()->after('adult_price');
            $table->index(['international_tour_pricing_id'], 'idx_itd_pricing');
        });

        DB::statement('UPDATE international_tour_dates SET child_price = child_price_1');

        Schema::table('international_tour_dates', function (Blueprint $table) {
            $table->dropColumn([
                'single_supplement', 'extra_bed_price', 'child_price_1', 'child_price_2',
                'room_capacity', 'infant_min_age', 'use_custom_payment_plan',
            ]);
        });
    }
};
