<?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 {
        // Phụ lục hợp đồng
        Schema::create('appendices', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('contract_id')->constrained('contracts')->cascadeOnDelete();
            $table->foreignUuid('product_id')->constrained('products')->cascadeOnDelete();
            $table->string('type');
            $table->integer('apply_from_order'); // Kế thừa từ CN số mấy
            $table->date('signing_date');
            $table->jsonb('custom_data')->nullable();
            $table->timestamps();
        });

        // Quyết toán & Sổ đỏ
        Schema::create('settlements', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('product_id')->constrained('products')->cascadeOnDelete();
            $table->string('type'); // MONG, THAN, CP THI CONG
            $table->decimal('temp_value', 15, 2);
            $table->decimal('final_value', 15, 2);
            $table->decimal('difference', 15, 2);
            $table->string('red_book_status');
            $table->date('issue_date')->nullable();
            $table->timestamps();
        });
    }

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