<?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('products', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('project_id')->constrained('projects')->cascadeOnDelete();
            $table->string('product_type'); // LAND, APARTMENT, SHOPHOUSE...
            
            // === TRƯỜNG CHUNG ===
            $table->string('code')->unique(); // Khu + Lô (STH03.01)
            $table->decimal('area', 12, 2);
            $table->decimal('price_per_unit', 15, 2);
            $table->decimal('total_price', 15, 2);
            
            // === TÀI CHÍNH BỔ SUNG ===
            $table->decimal('qsdd_value', 15, 2)->default(0);
            $table->decimal('foundation_temp_value', 15, 2)->default(0);
            $table->decimal('contract_temp_value', 15, 2)->default(0);
            
            // === KỸ THUẬT & XÂY DỰNG ===
            $table->string('adjacent_road')->nullable();
            $table->integer('frontage_count')->nullable();
            $table->integer('max_floors')->nullable();
            $table->decimal('building_density', 5, 2)->nullable();
            $table->string('construction_status')->nullable();
            
            // === HẠ TẦNG (NESTED) ===
            $table->text('infrastructure_raw_text')->nullable();
            $table->jsonb('infrastructure_status')->nullable();
            
            // === TRẠNG THÁI ===
            $table->string('status')->default('Đang mở bán');
            $table->string('red_book_status')->default('Chưa có dữ liệu');
            
            // === LINH HOẠT ===
            $table->jsonb('custom_data')->nullable();
            
            $table->timestamps();
        });
    }
    public function down(): void { Schema::dropIfExists('products'); }
};
