<?php

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

/**
 * Menambah kolom pengiriman ke tabel orders yang sudah ada.
 * TIDAK mengubah kolom / index yang sudah ada.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            // Nomor resi — bisa di-generate otomatis atau diisi manual oleh admin
            $table->string('tracking_number', 50)->nullable()->after('order_note');

            // Kurir yang dipilih saat checkout (jne_reg, jne_yes, jnt_reg, dst.)
            $table->string('courier_code', 30)->nullable()->after('tracking_number');

            // Nama display kurir, misal "JNE - Reguler"
            $table->string('courier_name', 100)->nullable()->after('courier_code');

            // Ongkos kirim (rupiah), 0 jika gratis ongkir
            $table->unsignedInteger('shipping_cost')->default(0)->after('courier_name');

            // Total berat pesanan dalam gram (dihitung dari produk)
            $table->unsignedInteger('total_weight')->default(0)->after('shipping_cost');

            // Estimasi hari pengiriman, misal "2-3 hari kerja"
            $table->string('estimated_days', 30)->nullable()->after('total_weight');

            // Kode zona pengiriman (untuk audit), misal "jawa", "luar_jawa", "papua"
            $table->string('shipping_zone', 30)->nullable()->after('estimated_days');

            // ID alamat yang dipakai saat checkout (foreign key ke addresses.id)
            $table->unsignedBigInteger('address_id')->nullable()->after('shipping_zone');

            $table->foreign('address_id')->references('id')->on('addresses')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->dropForeign(['address_id']);
            $table->dropColumn([
                'tracking_number',
                'courier_code',
                'courier_name',
                'shipping_cost',
                'total_weight',
                'estimated_days',
                'shipping_zone',
                'address_id',
            ]);
        });
    }
};
