<?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 {
        // Mẫu thanh toán
        Schema::create('payment_templates', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('project_id')->constrained('projects')->cascadeOnDelete();
            $table->string('name');
            $table->boolean('is_default')->default(false);
            $table->timestamps();
        });

        // Lịch trình thanh toán
        Schema::create('payment_schedules', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('contract_id')->constrained('contracts')->cascadeOnDelete();
            $table->foreignUuid('template_id')->constrained('payment_templates')->cascadeOnDelete();
            $table->timestamps();
        });

        // Chi tiết từng đợt thanh toán
        Schema::create('payment_schedule_items', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('template_id')->nullable()->constrained('payment_templates')->cascadeOnDelete();
            $table->foreignUuid('schedule_id')->nullable()->constrained('payment_schedules')->cascadeOnDelete();
            $table->integer('installment_no');
            $table->decimal('amount', 15, 2)->nullable();
            $table->decimal('percentage', 5, 2)->nullable();
            
            // --- LOGIC NGÀY ĐẾN HẠN ---
            $table->integer('days_after_signing')->nullable();   // Cách 1: X ngày sau ngày ký
            $table->integer('days_after_previous')->nullable();  // Cách 2: X ngày sau đợt trước
            $table->date('due_date')->nullable();                // Cách 3: Ngày chính xác
            
            $table->string('type'); // QSDD, MONG, THAN...
            $table->timestamps();
        });

        // Phiếu thu thực tế
        Schema::create('payments', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('contract_id')->constrained('contracts')->cascadeOnDelete();
            $table->foreignUuid('schedule_item_id')->nullable()->constrained('payment_schedule_items')->nullOnDelete();
            $table->decimal('amount', 15, 2);
            $table->date('paid_date');
            $table->string('receipt_number')->nullable();
            $table->string('method')->default('Chuyển khoản');
            $table->jsonb('metadata')->nullable();
            $table->timestamps();
        });

        // Tiền phạt chậm nộp
        Schema::create('payment_fines', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('contract_id')->constrained('contracts')->cascadeOnDelete();
            $table->decimal('amount', 15, 2);
            $table->string('reason');
            $table->date('due_date');
            $table->date('paid_date')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void {
        Schema::dropIfExists('payment_fines');
        Schema::dropIfExists('payments');
        Schema::dropIfExists('payment_schedule_items');
        Schema::dropIfExists('payment_schedules');
        Schema::dropIfExists('payment_templates');
    }
};
