<?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) {
            $table->string('invoice_prefix', 20)->nullable()->after('invoice_number');
            $table->unsignedInteger('invoice_seq')->nullable()->after('invoice_prefix');
            $table->string('po_no', 50)->nullable()->after('due_date');
            $table->date('po_date')->nullable()->after('po_no');
            $table->string('payment_mode', 20)->nullable()->after('po_date');
            $table->string('payment_terms', 50)->nullable()->after('payment_mode');
            $table->string('sales_person', 100)->nullable()->after('payment_terms');
            $table->decimal('excise_percent', 8, 2)->default(0)->after('sales_person');
            $table->decimal('excise_amount', 15, 2)->default(0)->after('excise_percent');
            $table->decimal('shipping_charges', 15, 2)->default(0)->after('discount_amount');
            $table->decimal('discount_on_total', 15, 2)->default(0)->after('shipping_charges');
            $table->decimal('discount_all_percent', 8, 2)->default(0)->after('discount_on_total');
            $table->decimal('custom_amount', 15, 2)->default(0)->after('discount_all_percent');
            $table->decimal('advance_payment', 15, 2)->default(0)->after('custom_amount');
            $table->boolean('has_shipping')->default(false)->after('advance_payment');
            $table->boolean('has_discount_total')->default(false)->after('has_shipping');
            $table->boolean('has_discount_all')->default(false)->after('has_discount_total');
            $table->boolean('has_custom_amount')->default(false)->after('has_discount_all');
            $table->boolean('has_advance_payment')->default(false)->after('has_custom_amount');
            $table->boolean('has_transport_eway')->default(false)->after('has_advance_payment');
            $table->text('transport_eway')->nullable()->after('has_transport_eway');
            $table->text('private_notes')->nullable()->after('terms');
        });

        Schema::table('invoice_items', function (Blueprint $table) {
            $table->text('description')->nullable()->after('item_name');
            $table->decimal('discount_percent', 8, 2)->default(0)->after('discount');
        });
    }

    public function down(): void
    {
        Schema::table('invoice_items', function (Blueprint $table) {
            $table->dropColumn(['description', 'discount_percent']);
        });

        Schema::table('invoices', function (Blueprint $table) {
            $table->dropColumn([
                'invoice_prefix', 'invoice_seq', 'po_no', 'po_date', 'payment_mode',
                'payment_terms', 'sales_person', 'excise_percent', 'excise_amount',
                'shipping_charges', 'discount_on_total', 'discount_all_percent',
                'custom_amount', 'advance_payment', 'has_shipping', 'has_discount_total',
                'has_discount_all', 'has_custom_amount', 'has_advance_payment',
                'has_transport_eway', 'transport_eway', 'private_notes',
            ]);
        });
    }
};
