<?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::create('orders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('kurir_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('address_id')->constrained('addresses')->cascadeOnDelete();

            $table->string('order_number')->unique();
            $table->string('transaction_id')->nullable(); // Dari Midtrans
            
            $table->decimal('total_price', 12, 2);      // Total Akhir (Barang + Ongkir)
            $table->decimal('shipping_cost', 12, 2);    // Biaya Ongkir Flat Rate
            $table->string('shipping_method');          // Nama Layanan (Express/Reguler)
            
            $table->text('shipping_address');           // Snapshot alamat
            $table->text('notes')->nullable();          // Catatan dari Customer
            
            $table->string('status')->default('pending'); 
            $table->string('payment_method')->nullable();
            $table->string('payment_type')->nullable(); // Gopay/QRIS/Bank
            $table->string('snap_token')->nullable();

            $table->timestamp('confirmed_at')->nullable();
            $table->timestamp('shipped_at')->nullable(); 
            $table->timestamp('delivered_at')->nullable(); 
            $table->string('proof_of_delivery')->nullable(); 
            $table->text('admin_note')->nullable(); 

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

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('orders');
    }
};