<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('ac_payment_vouchers', function (Blueprint $table) {
            $table->id();
            $table->string('voucher_number')->unique();
            $table->foreignId('customer_id')->nullable()->constrained()->onDelete('cascade');
            $table->foreignId('subscription_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('payment_method_id')->constrained();
            $table->foreignId('created_by')->constrained('users');
//            $table->foreignId('approved_by')->nullable()->constrained('users');

            $table->decimal('amount', 10, 2);
            $table->date('payment_date');
            $table->enum('status', ['active', 'canceled'])->default('active');
            $table->enum('type', ['internal', 'external'])->default('internal');
//            $table->string('expense_category')->nullable();

            $table->string('reference_number')->nullable();
            $table->string('payee_name')->nullable();
            $table->text('notes')->nullable();

            $table->text('cancellation_reason')->nullable();
            $table->timestamp('canceled_at')->nullable();
            $table->foreignId('canceled_by')->nullable()->constrained('users');
            $table->foreignId('refunded_receipt_id')->nullable()->constrained('ac_receipt_vouchers');

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

            // Indexes
            $table->index('voucher_number');
            $table->index('payment_date');
            $table->index('status');
            $table->index('type');
            $table->index(['customer_id', 'payment_date']);
        });
    }

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