<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('purchase_bills', function (Blueprint $table) {
            $table->foreignId('purchase_order_id')->nullable()->after('vendor_id')->constrained()->nullOnDelete();
            $table->string('bill_prefix', 20)->nullable()->after('bill_number');
            $table->unsignedInteger('bill_seq')->nullable()->after('bill_prefix');
            $table->string('po_number', 100)->nullable()->after('due_date');
            $table->date('po_date')->nullable()->after('po_number');
            $table->string('vendor_invoice_number', 100)->nullable()->after('po_date');
            $table->date('payment_date')->nullable()->after('paid_amount');
            $table->string('place_of_supply', 100)->nullable()->after('status');
            $table->boolean('is_igst')->default(false)->after('place_of_supply');
            $table->string('tax_on', 20)->default('bill_to')->after('is_igst');
            $table->decimal('taxable_amount', 15, 2)->default(0)->after('subtotal');
            $table->decimal('discount_amount', 15, 2)->default(0)->after('taxable_amount');
            $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->string('discount_all_type', 10)->default('percent')->after('discount_all_percent');
            $table->decimal('discount_all_value', 15, 2)->default(0)->after('discount_all_type');
            $table->decimal('custom_amount', 15, 2)->default(0)->after('discount_all_value');
            $table->string('custom_amount_label', 100)->nullable()->after('custom_amount');
            $table->decimal('advance_payment', 15, 2)->default(0)->after('custom_amount_label');
            $table->decimal('amount_due', 15, 2)->default(0)->after('grand_total');
            $table->decimal('cgst_amount', 15, 2)->default(0)->after('total_tax');
            $table->decimal('sgst_amount', 15, 2)->default(0)->after('cgst_amount');
            $table->decimal('igst_amount', 15, 2)->default(0)->after('sgst_amount');
            $table->decimal('round_off', 15, 2)->default(0)->after('igst_amount');
            $table->boolean('has_shipping')->default(false)->after('round_off');
            $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_total_quantity')->default(false)->after('has_advance_payment');
            $table->string('total_quantity_unit', 20)->nullable()->after('has_total_quantity');
            $table->decimal('total_quantity', 15, 3)->nullable()->after('total_quantity_unit');
            $table->boolean('show_cess')->default(false)->after('total_quantity');
            $table->boolean('reverse_charge')->default(false)->after('show_cess');
            $table->string('shipping_mode', 30)->nullable()->after('reverse_charge');
            $table->text('terms')->nullable()->after('notes');
            $table->text('private_notes')->nullable()->after('terms');
            $table->string('share_token', 64)->nullable()->unique()->after('private_notes');
        });

        DB::statement("ALTER TABLE purchase_bills MODIFY status VARCHAR(20) NOT NULL DEFAULT 'draft'");
        DB::table('purchase_bills')->where('status', 'received')->update(['status' => 'open']);
        DB::table('purchase_bills')->where('status', 'cancelled')->update(['status' => 'void']);

        Schema::table('purchase_bill_items', function (Blueprint $table) {
            $table->text('description')->nullable()->after('item_name');
            $table->string('unit', 20)->default('NOS')->after('hsn_sac');
            $table->decimal('discount_percent', 8, 2)->default(0)->after('rate');
            $table->decimal('discount', 15, 2)->default(0)->after('discount_percent');
            $table->decimal('cgst_rate', 5, 2)->default(0)->after('tax_rate');
            $table->decimal('sgst_rate', 5, 2)->default(0)->after('cgst_rate');
            $table->decimal('igst_rate', 5, 2)->default(0)->after('sgst_rate');
            $table->decimal('cgst_amount', 15, 2)->default(0)->after('tax_amount');
            $table->decimal('sgst_amount', 15, 2)->default(0)->after('cgst_amount');
            $table->decimal('igst_amount', 15, 2)->default(0)->after('sgst_amount');
        });
    }

    public function down(): void
    {
        Schema::table('purchase_bill_items', function (Blueprint $table) {
            $table->dropColumn([
                'description', 'unit', 'discount_percent', 'discount',
                'cgst_rate', 'sgst_rate', 'igst_rate',
                'cgst_amount', 'sgst_amount', 'igst_amount',
            ]);
        });

        Schema::table('purchase_bills', function (Blueprint $table) {
            $table->dropForeign(['purchase_order_id']);
            $table->dropColumn([
                'purchase_order_id', 'bill_prefix', 'bill_seq', 'po_number', 'po_date',
                'vendor_invoice_number', 'payment_date', 'place_of_supply', 'is_igst', 'tax_on',
                'taxable_amount', 'discount_amount', 'shipping_charges', 'discount_on_total',
                'discount_all_percent', 'discount_all_type', 'discount_all_value',
                'custom_amount', 'custom_amount_label', 'advance_payment', 'amount_due',
                'cgst_amount', 'sgst_amount', 'igst_amount', 'round_off',
                'has_shipping', 'has_discount_total', 'has_discount_all', 'has_custom_amount',
                'has_advance_payment', 'has_total_quantity', 'total_quantity_unit', 'total_quantity',
                'show_cess', 'reverse_charge', 'shipping_mode', 'terms', 'private_notes', 'share_token',
            ]);
        });
    }
};
