<?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
    {
        // Transactions = money received from customer (header).
        Schema::create('transactions', function (Blueprint $table) {
            $table->id();
            $table->string('transaction_number', 30)->unique(); // TRN-NNNNNN
            $table->foreignId('customer_id')->nullable()->constrained('customers')->cascadeOnUpdate()->nullOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('warehouses')->cascadeOnUpdate()->nullOnDelete();
            $table->foreignId('payment_method_id')->constrained('payment_methods')->cascadeOnUpdate()->restrictOnDelete();
            $table->foreignId('status_id')->constrained('statuses')->cascadeOnUpdate()->restrictOnDelete();
            $table->decimal('amount', 14, 2);
            $table->decimal('used_amount', 14, 2)->default(0);
            $table->decimal('usable_amount', 14, 2)->default(0);
            $table->decimal('usable_pending', 14, 2)->default(0);
            $table->string('reference', 200)->nullable(); // bank ref / cheque combined ref
            $table->text('note')->nullable();
            $table->timestamp('taken_on')->nullable();

            // Cheque-specific fields (NULL for non-cheque)
            $table->string('cheque_number', 50)->nullable();
            $table->string('cheque_bank', 100)->nullable();
            $table->string('cheque_branch_code', 20)->nullable();
            $table->date('cheque_date')->nullable();
            $table->timestamp('cheque_reminder_sent_at')->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(['customer_id', 'status_id']);
            $table->index('cheque_date');
        });

        // Transaction claims = allocation of slices to invoices.
        Schema::create('transaction_claims', function (Blueprint $table) {
            $table->id();
            $table->string('claim_number', 30)->unique(); // CLM-NNNNNN
            $table->foreignId('transaction_id')->constrained('transactions')->cascadeOnUpdate()->cascadeOnDelete();
            $table->foreignId('invoice_id')->constrained('invoices')->cascadeOnUpdate()->cascadeOnDelete();
            $table->decimal('amount', 14, 2);
            $table->text('note')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->index(['transaction_id', 'invoice_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('transaction_claims');
        Schema::dropIfExists('transactions');
    }
};
