<?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('contracts', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('product_id')->constrained('products')->cascadeOnDelete();
            $table->integer('transfer_order')->default(0); 
            $table->string('contract_type')->default('HĐMB');
            $table->string('contract_number')->unique();
            $table->date('signing_date')->nullable();
            $table->string('status')->default('Đang hiệu lực');
            $table->decimal('total_value', 15, 2);
            $table->decimal('paid_amount', 15, 2)->default(0);
            $table->decimal('remaining_amount', 15, 2)->default(0);
            $table->decimal('excess_amount', 15, 2)->default(0); // Tiền dư từ đợt trước
            $table->jsonb('metadata')->nullable();
            $table->timestamps();
        });
    }
    public function down(): void { Schema::dropIfExists('contracts'); }
};
