<?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::create('shipping_rates', function (Blueprint $table) {
            $table->id();
            
            // Relasi Kota Asal
            $table->unsignedBigInteger('origin_city_id');
            
            // Relasi Kelurahan Tujuan (LANGSUNG PAKAI VILLAGE)
            $table->unsignedBigInteger('destination_village_id');

            $table->string('service_name');
            $table->string('service_type')->nullable();
            $table->decimal('price', 15, 2);
            $table->integer('min_weight')->default(1);
            $table->string('etd');
            
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();

            // Foreign Keys
            $table->foreign('origin_city_id')->references('id')->on('m_cities')->onDelete('cascade');
            
            // Pastikan nama tabel referensinya benar: 'm_villages' atau 'villages'?
            $table->foreign('destination_village_id')->references('id')->on('m_villages')->onDelete('cascade');
            
            $table->foreign('created_by')->references('id')->on('users');
            $table->foreign('updated_by')->references('id')->on('users');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('shipping_rates');
    }
};