<?php

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

/**
 * Satu order = satu record payment aktif.
 * Jika pembayaran ditolak dan customer upload ulang,
 * record payment lama di-update (bukan insert baru).
 *
 * DESAIN verified_by:
 * - User ID admin yang melakukan verifikasi.
 * - nullOnDelete: jika admin dihapus, payment tetap ada.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('payments', function (Blueprint $table) {
            $table->id();

            $table->foreignId('order_id')
                ->unique() // one-to-one: satu order satu payment record
                ->constrained('orders')
                ->cascadeOnDelete();

            $table->enum('method', ['transfer', 'qris', 'cod']);
            $table->decimal('amount', 12, 2);

            // Detail transfer (opsional, untuk COD tidak perlu)
            $table->string('sender_bank')->nullable();    // bank pengirim
            $table->string('sender_name')->nullable();    // nama pengirim
            $table->string('proof_image')->nullable();    // path bukti transfer/qris

            $table->enum('status', ['pending', 'verified', 'rejected'])->default('pending');

            // Verifikasi
            $table->foreignId('verified_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            $table->timestamp('verified_at')->nullable();
            $table->text('rejection_notes')->nullable(); // alasan jika rejected

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

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