<?php

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

return new class extends Migration
{
    public function up()
    {
        Schema::create('transactions', function (Blueprint $table) {
            $table->id();
            $table->string('invoice_number')->unique();
            $table->string('reference')->nullable()->unique(); // Tripay reference
            $table->string('merchant_ref')->nullable(); // Our reference for Tripay
            
            // Customer Info
            $table->string('customer_email')->nullable();
            $table->string('customer_phone')->nullable();
            $table->string('customer_name')->nullable();
            
            // Product Info
            $table->foreignId('product_id')->constrained()->onDelete('cascade');
            $table->string('product_name');
            $table->string('product_code'); // SKU
            
            // Target Info (User ID, Phone Number, etc)
            $table->string('customer_data'); // User ID / No HP / Email
            
            // Pricing Breakdown
            $table->decimal('base_price', 15, 2); // Harga dari VIP Reseller
            $table->decimal('margin_amount', 15, 2)->default(0); // Profit kita
            $table->decimal('product_price', 15, 2); // base_price + margin
            $table->decimal('payment_fee', 15, 2)->default(0); // Fee payment gateway
            $table->decimal('total_amount', 15, 2); // product_price + payment_fee
            
            // Status
            $table->enum('status', ['pending', 'paid', 'processing', 'success', 'failed', 'refund', 'expired'])->default('pending');
            $table->enum('payment_status', ['UNPAID', 'PAID', 'EXPIRED', 'FAILED'])->default('UNPAID');
            
            // Payment Gateway (Tripay)
            $table->string('payment_method')->nullable(); // QRIS, OVO, DANA, etc
            $table->string('payment_name')->nullable(); // Nama metode pembayaran
            $table->string('checkout_url')->nullable(); // Tripay checkout URL
            $table->text('payment_instructions')->nullable(); // Instruksi pembayaran (JSON)
            $table->timestamp('paid_at')->nullable();
            $table->timestamp('expired_at')->nullable();
            
            // Provider Response (VIP Reseller)
            $table->string('trx_id')->nullable(); // VIP Reseller transaction ID
            $table->text('provider_response')->nullable();
            $table->text('provider_note')->nullable();
            $table->string('provider_status')->nullable();
            $table->string('provider_sn')->nullable(); // Serial Number / Voucher Code
            
            $table->timestamps();
            
            // Indexes
            $table->index('status');
            $table->index('payment_status');
            $table->index('invoice_number');
            $table->index('reference');
            $table->index('merchant_ref');
        });
    }

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