<?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('debit_notes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->enum('entity_type', ['client', 'vendor'])->default('client');
            $table->foreignId('customer_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('vendor_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('invoice_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('purchase_bill_id')->nullable()->constrained()->nullOnDelete();
            $table->string('debit_note_number', 50);
            $table->string('debit_note_prefix', 20)->nullable();
            $table->unsignedInteger('debit_note_seq')->nullable();
            $table->date('debit_note_date');
            $table->string('related_document_number', 50)->nullable();
            $table->date('related_document_date')->nullable();
            $table->string('reason', 50)->default('correction_in_invoice');
            $table->enum('status', ['draft', 'unpaid', 'paid', 'void'])->default('draft');
            $table->string('place_of_supply', 100)->nullable();
            $table->boolean('is_igst')->default(false);
            $table->string('tax_on', 20)->default('bill_to');
            $table->decimal('subtotal', 15, 2)->default(0);
            $table->decimal('taxable_amount', 15, 2)->default(0);
            $table->decimal('discount_amount', 15, 2)->default(0);
            $table->decimal('shipping_charges', 15, 2)->default(0);
            $table->decimal('discount_on_total', 15, 2)->default(0);
            $table->decimal('discount_all_percent', 8, 2)->default(0);
            $table->string('discount_all_type', 10)->default('percent');
            $table->decimal('discount_all_value', 15, 2)->default(0);
            $table->decimal('custom_amount', 15, 2)->default(0);
            $table->string('custom_amount_label', 100)->nullable();
            $table->decimal('advance_payment', 15, 2)->default(0);
            $table->boolean('has_shipping')->default(false);
            $table->boolean('has_discount_total')->default(false);
            $table->boolean('has_discount_all')->default(false);
            $table->boolean('has_custom_amount')->default(false);
            $table->boolean('has_advance_payment')->default(false);
            $table->boolean('has_total_quantity')->default(false);
            $table->string('total_quantity_unit', 20)->nullable();
            $table->decimal('total_quantity', 15, 3)->nullable();
            $table->boolean('show_cess')->default(false);
            $table->string('shipping_mode', 30)->nullable();
            $table->decimal('total_tax', 15, 2)->default(0);
            $table->decimal('cgst_amount', 15, 2)->default(0);
            $table->decimal('sgst_amount', 15, 2)->default(0);
            $table->decimal('igst_amount', 15, 2)->default(0);
            $table->decimal('round_off', 15, 2)->default(0);
            $table->decimal('grand_total', 15, 2)->default(0);
            $table->decimal('amount_due', 15, 2)->default(0);
            $table->decimal('paid_amount', 15, 2)->default(0);
            $table->text('notes')->nullable();
            $table->text('terms')->nullable();
            $table->text('private_notes')->nullable();
            $table->string('share_token', 64)->nullable()->unique();
            $table->timestamps();

            $table->unique(['company_id', 'debit_note_number']);
        });

        Schema::create('debit_note_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('debit_note_id')->constrained()->cascadeOnDelete();
            $table->foreignId('item_id')->nullable()->constrained()->nullOnDelete();
            $table->string('item_name');
            $table->text('description')->nullable();
            $table->string('hsn_sac', 20)->nullable();
            $table->string('unit', 20)->default('NOS');
            $table->decimal('quantity', 15, 3)->default(1);
            $table->decimal('rate', 15, 2)->default(0);
            $table->decimal('discount_percent', 8, 2)->default(0);
            $table->decimal('discount', 15, 2)->default(0);
            $table->decimal('tax_rate', 5, 2)->default(0);
            $table->decimal('cgst_rate', 5, 2)->default(0);
            $table->decimal('sgst_rate', 5, 2)->default(0);
            $table->decimal('igst_rate', 5, 2)->default(0);
            $table->decimal('tax_amount', 15, 2)->default(0);
            $table->decimal('cgst_amount', 15, 2)->default(0);
            $table->decimal('sgst_amount', 15, 2)->default(0);
            $table->decimal('igst_amount', 15, 2)->default(0);
            $table->decimal('amount', 15, 2)->default(0);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('debit_note_items');
        Schema::dropIfExists('debit_notes');
    }
};
