<?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('income_tax_declarations', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->string('financial_year', 10);
            $table->string('regime', 10)->default('new');
            $table->unsignedBigInteger('income_tax_plan_id')->nullable();
            $table->decimal('gross_annual_estimate', 14, 2)->default(0);
            $table->decimal('total_deductions_declared', 14, 2)->default(0);
            $table->decimal('total_deductions_verified', 14, 2)->default(0);
            $table->decimal('taxable_income', 14, 2)->default(0);
            $table->decimal('annual_tax', 14, 2)->default(0);
            $table->decimal('monthly_tds', 14, 2)->default(0);
            $table->string('status', 30)->default('draft');
            $table->timestamp('submitted_at')->nullable();
            $table->timestamp('verified_at')->nullable();
            $table->unsignedBigInteger('verified_by')->nullable();
            $table->text('hr_remarks')->nullable();
            $table->timestamps();

            $table->unique(['company_id', 'user_id', 'financial_year'], 'it_declarations_unique');
        });

        Schema::create('income_tax_declaration_items', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('income_tax_declaration_id')->index();
            $table->string('section_code', 30);
            $table->string('section_name', 191);
            $table->decimal('declared_amount', 14, 2)->default(0);
            $table->decimal('verified_amount', 14, 2)->nullable();
            $table->decimal('section_limit', 14, 2)->nullable();
            $table->string('proof_path')->nullable();
            $table->string('proof_status', 30)->default('pending');
            $table->text('review_remarks')->nullable();
            $table->timestamps();
        });

        Schema::create('employee_tds_records', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->string('financial_year', 10);
            $table->unsignedBigInteger('payroll_cycle_id')->nullable()->index();
            $table->unsignedBigInteger('employee_payroll_id')->nullable()->index();
            $table->unsignedSmallInteger('month');
            $table->unsignedSmallInteger('year');
            $table->decimal('tds_amount', 14, 2)->default(0);
            $table->decimal('cumulative_tds', 14, 2)->default(0);
            $table->decimal('gross_for_month', 14, 2)->default(0);
            $table->timestamps();

            $table->unique(['employee_payroll_id'], 'employee_tds_payroll_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('employee_tds_records');
        Schema::dropIfExists('income_tax_declaration_items');
        Schema::dropIfExists('income_tax_declarations');
    }
};
