<?php

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

/**
 * Tabel orders adalah jantung transaksi.
 *
 * DESAIN customer (SNAPSHOT):
 * - customer_type: 'user' atau 'guest'.
 * - Jika 'user': user_id terisi, guest_customer_id null.
 * - Jika 'guest': guest_customer_id terisi, user_id null.
 * - customer_name, customer_phone, delivery_address_snapshot
 *   SELALU di-copy dari profil customer saat checkout.
 *   Jika customer mengubah nama, HP, atau alamat besok,
 *   pesanan lama TIDAK IKUT BERUBAH. Ini desain yang disengaja.
 *
 * DESAIN assigned_admin:
 * - Multi-admin: setiap order bisa di-assign ke satu admin.
 * - Nullable: order baru belum tentu langsung di-assign.
 * - nullOnDelete: jika admin dihapus, order tetap ada (assigned jadi null).
 *
 * DESAIN financial:
 * - Semua nilai keuangan di-snapshot saat order dibuat.
 * - Subtotal = total item sebelum diskon dan ongkir.
 * - Grand total = subtotal - discount + delivery_cost.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->string('order_number', 25)->unique(); // format: TBB-YYYYMMDD-XXXX

            // ── Relasi Customer ──────────────────────────────────────────
            $table->enum('customer_type', ['user', 'guest']);

            $table->foreignId('user_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();

            $table->foreignId('guest_customer_id')
                ->nullable()
                ->constrained('guest_customers')
                ->nullOnDelete();

            // ── Snapshot Customer saat Checkout ──────────────────────────
            // Tiga kolom ini di-copy dari profil customer tepat saat order dibuat.
            // Perubahan profil di kemudian hari tidak akan mempengaruhi order ini.
            $table->string('customer_name');               // snapshot nama
            $table->string('customer_phone', 20);          // snapshot nomor HP
            $table->text('delivery_address_snapshot');     // snapshot alamat pengiriman

            // ── Admin yang menangani ─────────────────────────────────────
            $table->foreignId('assigned_admin_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();

            // ── Pengiriman ───────────────────────────────────────────────
            $table->foreignId('delivery_area_id')
                ->nullable()
                ->constrained('delivery_areas')
                ->nullOnDelete();

            $table->decimal('delivery_cost', 10, 2)->default(0);

            // ── Voucher ──────────────────────────────────────────────────
            $table->foreignId('voucher_id')
                ->nullable()
                ->constrained('vouchers')
                ->nullOnDelete();

            $table->decimal('discount_amount', 10, 2)->default(0);

            // ── Finansial ────────────────────────────────────────────────
            $table->decimal('subtotal', 12, 2);      // total item (sebelum diskon + ongkir)
            $table->decimal('grand_total', 12, 2);   // subtotal - discount + delivery_cost

            // ── Status & Pembayaran ──────────────────────────────────────
            $table->enum('status', [
                'pending',     // menunggu pembayaran
                'paid',        // bukti diterima, menunggu verifikasi
                'processing',  // pembayaran verified, sedang disiapkan
                'delivering',  // dalam pengiriman
                'completed',   // selesai
                'cancelled',   // dibatalkan
            ])->default('pending');

            $table->enum('payment_method', ['transfer', 'qris', 'cod']);

            $table->text('customer_notes')->nullable(); // catatan dari customer
            $table->text('admin_notes')->nullable();    // catatan dari admin

            $table->timestamp('payment_deadline')->nullable(); // deadline bayar (transfer/qris)

            $table->timestamps();
            $table->softDeletes();
        });
    }

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