<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Modules\Accounts\Enums\TransactionTypeEnum;
use Modules\Core\Enums\VatTypeEnum;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('vat_transactions', function (Blueprint $table) {
            $table->id();
            $table->date('transaction_date');
            //source_type (purchase_invoice, billing, credit_note, etc) and source_id
            $table->morphs('source');
            $table->enum('transaction_type', TransactionTypeEnum::casesAsArray());
            //Creates itemable_type (products, services, or any other line items) and itemable_id
            $table->nullableMorphs('itemable');
            $table->decimal('taxable_amount', 20, 2)->default(0);
            $table->enum('vat_type', VatTypeEnum::casesAsArray());
            $table->decimal('vat_rate', 5, 2)->default(0);
            $table->decimal('vat_amount', 20, 2)->default(0);
            $table->foreignId('account_id')->constrained('chart_of_accounts');
            $table->foreignId('fiscal_year_id')->constrained('fiscal_years');
            $table->foreignId('accounting_period_id')->constrained('accounting_periods');
            $table->boolean('is_settled')->default(false);
            $table->foreignId('vat_settlement_id')->nullable()->constrained()->nullOnDelete();

            $table->timestamps();

            $table->index(['transaction_date', 'transaction_type']);
        });
    }

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