<?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('quotations', function (Blueprint $table) {
            $table->string('quote_prefix', 20)->nullable()->after('quote_number');
            $table->unsignedInteger('quote_seq')->nullable()->after('quote_prefix');
            $table->string('po_label', 50)->nullable()->after('valid_until');
            $table->string('po_no', 50)->nullable()->after('po_label');
            $table->date('po_date')->nullable()->after('po_no');
            $table->decimal('discount_amount', 15, 2)->default(0)->after('subtotal');
            $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->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->string('shipping_mode', 30)->nullable()->after('has_advance_payment');
            $table->text('terms')->nullable()->after('notes');
            $table->text('private_notes')->nullable()->after('terms');
        });

        Schema::table('quotation_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');
        });
    }

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

        Schema::table('quotations', function (Blueprint $table) {
            $table->dropColumn([
                'quote_prefix', 'quote_seq', 'po_label', 'po_no', 'po_date',
                'discount_amount', 'shipping_charges', 'discount_on_total',
                'discount_all_percent', 'discount_all_type', 'discount_all_value',
                'custom_amount', 'custom_amount_label', 'advance_payment',
                'has_shipping', 'has_discount_total', 'has_discount_all',
                'has_custom_amount', 'has_advance_payment', 'shipping_mode',
                'terms', 'private_notes',
            ]);
        });
    }
};
