<?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('vouchers', function (Blueprint $table) {
            $table->id();
            $table->string('voucher_no')->unique();
            $table->enum('type', ['receipt', 'payment', 'journal', 'contra'])->index();
            $table->date('voucher_date');
            $table->foreignId('account_id')->constrained('accounts')->cascadeOnDelete();
            $table->foreignId('party_id')->nullable()->constrained('parties')->nullOnDelete();
            $table->decimal('amount', 15, 2)->default(0);
            $table->string('mode')->nullable(); // cash, cheque, bank transfer
            $table->string('cheque_no')->nullable();
            $table->date('cheque_date')->nullable();
            $table->text('description')->nullable();
            $table->enum('status', ['draft', 'posted', 'cancelled'])->default('posted');
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();
            $table->index(['voucher_date', 'type']);
        });
    }

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