<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('facility_rent_payments', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->foreignId('branch_id')->nullable()->constrained('branches');
            $table->foreignId('facility_id')->constrained('facilities');
            $table->string('period', 7); // YYYY-MM
            $table->bigInteger('amount');
            $table->string('payment_method', 30)->default('cheque');
            $table->string('cheque_number', 50)->nullable();
            $table->date('cheque_date')->nullable();
            $table->string('bank_name', 100)->nullable();
            $table->string('recipient_name', 255)->nullable();
            $table->date('payment_date');
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->constrained('users');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['academy_id', 'facility_id', 'period']);
            $table->unique(['academy_id', 'facility_id', 'period']);
        });

        DB::statement("ALTER TABLE facility_rent_payments ADD CONSTRAINT facility_rent_payments_payment_method_check CHECK (payment_method IN ('cash', 'card', 'bank_transfer', 'wallet', 'online', 'cheque', 'other'))");
    }

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