<?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('bill_payments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('customer_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('recorded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('reference')->unique();
            $table->string('service_type')->index();
            $table->string('provider')->index();
            $table->string('customer_identifier')->index();
            $table->string('customer_name')->nullable();
            $table->string('package_name')->nullable();
            $table->decimal('amount', 14, 2);
            $table->decimal('fee', 14, 2)->default(0);
            $table->decimal('commission', 14, 2)->default(0);
            $table->decimal('total_amount', 14, 2);
            $table->string('payment_method')->default('cash')->index();
            $table->string('status')->default('successful')->index();
            $table->string('provider_reference')->nullable()->index();
            $table->text('token')->nullable();
            $table->text('notes')->nullable();
            $table->timestamp('processed_at')->nullable()->index();
            $table->timestamps();
            $table->index(['tenant_id', 'service_type', 'created_at']);
            $table->index(['tenant_id', 'status', 'created_at']);
            $table->index(['tenant_id', 'customer_identifier']);
        });
    }

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