<?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('billing_profiles', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->unsignedTinyInteger('invoice_day');
            $table->unsignedTinyInteger('send_day');
            $table->unsignedTinyInteger('warning_day');
            $table->unsignedTinyInteger('isolation_day');
            $table->string('tax_type', 20)->default('non_tax');
            $table->decimal('tax_rate', 5, 2)->default(0);
            $table->string('status', 20)->default('active');
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->unique(['tenant_id', 'name']);
            $table->index(['tenant_id', 'status']);
        });

        Schema::table('customers', function (Blueprint $table) {
            $table->foreign('billing_profile_id')
                ->references('id')
                ->on('billing_profiles')
                ->nullOnDelete();
        });

        Schema::create('invoices', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('customer_id')->constrained()->restrictOnDelete();
            $table->foreignId('billing_profile_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('paid_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('invoice_number');
            $table->date('period_start');
            $table->date('period_end');
            $table->date('issue_date');
            $table->date('send_date')->nullable();
            $table->date('warning_date')->nullable();
            $table->date('isolation_date')->nullable();
            $table->decimal('subtotal', 15, 2);
            $table->string('tax_type', 20)->default('non_tax');
            $table->decimal('tax_rate', 5, 2)->default(0);
            $table->decimal('tax_amount', 15, 2)->default(0);
            $table->decimal('total', 15, 2);
            $table->decimal('paid_amount', 15, 2)->default(0);
            $table->decimal('balance', 15, 2);
            $table->string('status', 20)->default('draft');
            $table->string('payment_status', 20)->default('unpaid');
            $table->timestamp('paid_at')->nullable();
            $table->string('payment_method', 50)->nullable();
            $table->string('payment_reference')->nullable();
            $table->string('external_payment_id')->nullable()->index();
            $table->string('finance_reference')->nullable()->index();
            $table->string('notification_status', 30)->default('pending');
            $table->string('customer_code_snapshot');
            $table->string('customer_name_snapshot');
            $table->text('customer_address_snapshot')->nullable();
            $table->string('package_name_snapshot')->nullable();
            $table->json('profile_snapshot')->nullable();
            $table->json('metadata')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->unique(['tenant_id', 'invoice_number']);
            $table->index(['tenant_id', 'status', 'issue_date']);
            $table->index(['tenant_id', 'payment_status', 'isolation_date']);
            $table->index(['customer_id', 'period_start', 'period_end']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('invoices');
        Schema::table('customers', function (Blueprint $table) {
            $table->dropForeign(['billing_profile_id']);
        });
        Schema::dropIfExists('billing_profiles');
    }
};
