<?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
    {
        if (! Schema::hasTable('income_tax_plans')) {
            Schema::create('income_tax_plans', function (Blueprint $table) {
                $table->id();
                $table->string('plan_name', 100);
                $table->string('plan_code', 50);
                $table->boolean('allow_deductions')->default(true);
                $table->decimal('standard_deduction', 10, 2)->default(50000);
                $table->text('description')->nullable();
                $table->date('effective_from')->nullable();
                $table->date('effective_to')->nullable();
                $table->boolean('is_active')->default(true);
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('tax_slabs')) {
            Schema::create('tax_slabs', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('plan_id');
                $table->unsignedSmallInteger('slab_no')->nullable();
                $table->decimal('min_amount', 12, 2);
                $table->decimal('max_amount', 12, 2)->nullable();
                $table->decimal('tax_rate', 5, 2);
                $table->decimal('cess_rate', 5, 2)->default(4.00);
                $table->timestamps();
                $table->foreign('plan_id')->references('id')->on('income_tax_plans')->onDelete('cascade');
            });
        }

        if (! Schema::hasTable('deduction_sections')) {
            Schema::create('deduction_sections', function (Blueprint $table) {
                $table->id();
                $table->string('section_code', 20)->nullable();
                $table->string('section_name', 100)->nullable();
                $table->decimal('limit_amount', 12, 2)->nullable();
                $table->text('description')->nullable();
                $table->boolean('is_active')->default(true);
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('tax_slabs');
        Schema::dropIfExists('income_tax_plans');
        Schema::dropIfExists('deduction_sections');
    }
};
