<?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::table('invoices', function (Blueprint $table) {

            // GST type — gst or igst
            $table->enum('tax_type', ['gst', 'igst'])
                  ->default('gst')
                  ->after('final_amount');

            // Old gst_percent becomes optional
            $table->decimal('gst_percent', 5, 2)
                  ->nullable()
                  ->change();

            // Split GST %
            $table->decimal('cgst_percent', 5, 2)
                  ->nullable()
                  ->after('gst_percent');

            $table->decimal('sgst_percent', 5, 2)
                  ->nullable()
                  ->after('cgst_percent');

            // IGST %
            $table->decimal('igst_percent', 5, 2)
                  ->nullable()
                  ->after('sgst_percent');

            // Tax amount recalculation is the same
            // gst_amount and final_amount_with_gst already exist
        });
    }

    public function down(): void
    {
        Schema::table('invoices', function (Blueprint $table) {
            $table->dropColumn([
                'tax_type',
                'cgst_percent',
                'sgst_percent',
                'igst_percent',
            ]);
        });
    }
};
