<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('shipping_rates', function (Blueprint $table) {
            // 1. Hapus Foreign Key & Kolom Kota Tujuan (karena kurang spesifik)
            // Pastikan nama foreign key sesuai dengan yang ada di database Anda.
            // Biasanya formatnya: table_column_foreign
            $table->dropForeign(['destination_city_id']); 
            $table->dropColumn('destination_city_id');

            // 2. Tambahkan Kolom Kecamatan Tujuan (Lebih spesifik)
            $table->unsignedBigInteger('destination_district_id')->after('origin_city_id');
            
            // Tambahkan Foreign Key ke m_districts
            $table->foreign('destination_district_id')
                  ->references('id')
                  ->on('m_districts')
                  ->onDelete('cascade'); // Jika kecamatan dihapus, harga ikut terhapus

            // 3. Tambahkan kolom Minimum Berat (Penting untuk logistik)
            $table->integer('min_weight')->default(1)->after('price'); 
            
            // Opsional: Jika ingin deskripsi estimasi (e.g., "2-3 Hari")
            // Kolom 'etd' sudah ada, jadi aman.
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('shipping_rates', function (Blueprint $table) {
            // Balikkan proses jika rollback
            $table->dropForeign(['destination_district_id']);
            $table->dropColumn('destination_district_id');
            $table->dropColumn('min_weight');

            $table->unsignedBigInteger('destination_city_id')->after('origin_city_id');
            $table->foreign('destination_city_id')->references('id')->on('m_cities')->onDelete('cascade');
        });
    }
};