<?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('invoices', function (Blueprint $table) {
            $table->id();
            $table->ulid('uuid')->unique();
            $table->string('invoice_number', 30)->unique(); // INV-YYYY-NNNNNN
            $table->string('physical_reference', 50)->nullable()->index();
            $table->foreignId('branch_id')->nullable()->constrained('warehouses')->cascadeOnUpdate()->nullOnDelete();
            $table->foreignId('customer_id')->nullable()->constrained('customers')->cascadeOnUpdate()->nullOnDelete();
            $table->foreignId('delivery_address_id')->nullable()->constrained('delivery_addresses')->cascadeOnUpdate()->nullOnDelete();
            $table->foreignId('till_id')->nullable()->constrained('tills')->cascadeOnUpdate()->nullOnDelete();
            $table->foreignId('pos_session_id')->nullable()->constrained('pos_sessions')->cascadeOnUpdate()->nullOnDelete();
            $table->foreignId('sales_rep_id')->nullable()->constrained('sales_reps')->cascadeOnUpdate()->nullOnDelete();

            $table->enum('channel', ['till', 'sales']);
            $table->foreignId('order_status_id')->constrained('statuses')->cascadeOnUpdate()->restrictOnDelete();
            $table->foreignId('payment_status_id')->constrained('statuses')->cascadeOnUpdate()->restrictOnDelete();

            $table->decimal('subtotal', 14, 2)->default(0);
            $table->decimal('vat_total', 14, 2)->default(0);
            $table->decimal('total', 14, 2)->default(0);
            $table->decimal('discount', 14, 2)->default(0);
            $table->decimal('net_total', 14, 2)->default(0);
            $table->decimal('invoice_payable', 14, 2)->default(0);
            $table->decimal('paid_amount', 14, 2)->default(0);
            $table->decimal('balance', 14, 2)->default(0);

            $table->text('note')->nullable();

            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('deleted_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['branch_id', 'created_at']);
            $table->index(['customer_id', 'payment_status_id']);
            $table->index('channel');
        });

        Schema::create('invoice_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('invoice_id')->constrained('invoices')->cascadeOnUpdate()->cascadeOnDelete();
            $table->foreignId('product_id')->nullable()->constrained('products')->cascadeOnUpdate()->nullOnDelete();
            $table->string('product_name', 255);
            $table->string('product_sku', 60)->nullable();
            $table->string('product_barcode', 50)->nullable();
            $table->decimal('quantity', 12, 4);
            $table->decimal('unit_price', 12, 2);
            $table->decimal('vat_rate', 5, 2)->default(0);
            $table->decimal('vat_amount', 14, 2)->default(0);
            $table->decimal('line_total', 14, 2);
            $table->timestamps();
            $table->index('product_id');
        });

        Schema::create('invoice_audit_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('invoice_id')->constrained('invoices')->cascadeOnUpdate()->cascadeOnDelete();
            $table->foreignId('changed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('field', 100);
            $table->text('old_value')->nullable();
            $table->text('new_value')->nullable();
            $table->timestamp('created_at')->useCurrent();
            $table->index(['invoice_id', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('invoice_audit_logs');
        Schema::dropIfExists('invoice_items');
        Schema::dropIfExists('invoices');
    }
};
