<?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('customer_transactions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('customer_id')->constrained()->onDelete('cascade');
            $table->string('trx_no', 50)->nullable(); // ✅
            $table->enum('type', ['bill','payment','adjust']);
            $table->decimal('debit', 12, 2)->default(0);
            $table->decimal('credit', 12, 2)->default(0);
            $table->text('note')->nullable();
            $table->string('bill_no')->nullable();
            $table->string('bill_photo')->nullable();
            $table->string('payment_proof_photo')->nullable();
            $table->foreignId('user_account_id')->nullable()->constrained('user_accounts')->nullOnDelete();
            $table->dateTime('posted_at')->nullable();
            $table->timestamps();
            $table->unique(['user_id','trx_no']); // ✅ user-wise unique
            $table->index(['user_id','customer_id']);
            $table->index('type');
            $table->index('posted_at');
        });
    }

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