<?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('property_master', function (Blueprint $table) {

            $table->id('Prop_Id');

            $table->string('Prop_No',30)->unique();
            $table->string('Old_Prop_No',30)->nullable();

            $table->unsignedBigInteger('Ward_Id')->nullable();
            $table->unsignedBigInteger('Zone_Id')->nullable();
            $table->unsignedBigInteger('Owner_Id')->nullable();

            $table->string('Prop_Name',200)->nullable();
            $table->string('Plot_No',50)->nullable();
            $table->string('Survey_No',50)->nullable();
            $table->string('CTS_No',50)->nullable();

            $table->string('Address_Line1',255);
            $table->string('Address_Line2',255)->nullable();
            $table->string('Pincode',10)->nullable();

            $table->decimal('Latitude',11,8)->nullable();
            $table->decimal('Longitude',11,8)->nullable();

            $table->unsignedBigInteger('UsgTy_Id')->nullable();
            $table->unsignedBigInteger('ConsTy_Id')->nullable();
            $table->unsignedBigInteger('Occp_Id')->nullable();

            $table->decimal('Total_Area',10,2);
            $table->decimal('Built_Area',10,2);
            $table->decimal('Carpet_Area',10,2)->nullable();

            $table->smallInteger('Floor_No')->default(0);
            $table->smallInteger('Total_Floors')->default(1);

            $table->year('Year_Built')->nullable();
            $table->decimal('Age_Factor',5,2)->default(1.00);

            $table->decimal('Capital_Value',15,2)->nullable();
            $table->decimal('Annual_Rent_Value',12,2)->nullable();

            $table->boolean('Is_Corner')->default(0);
            $table->boolean('Has_Parking')->default(0);
            $table->decimal('Parking_Area',8,2)->nullable();

            $table->boolean('Is_Exempt')->default(0);
            $table->string('Exempt_Reason',255)->nullable();

            $table->enum('Status',[
                'Active',
                'Merged',
                'Bifurcated',
                'Demolished',
                'Disputed'
            ])->default('Active');

            $table->timestamps();
            $table->softDeletes();

            $table->foreign('Ward_Id')->references('Wrd_Id')->on('ward_master')->nullOnDelete();
            $table->foreign('Zone_Id')->references('Zon_Id')->on('zone_master')->nullOnDelete();
            $table->foreign('Owner_Id')->references('Owner_ID')->on('owner_master')->nullOnDelete();
            $table->foreign('UsgTy_Id')->references('UsgTy_Id')->on('property_usage_types')->nullOnDelete();
            $table->foreign('ConsTy_Id')->references('ConsTy_Id')->on('construction_types')->nullOnDelete();
            $table->foreign('Occp_Id')->references('Occp_Id')->on('occupancy_status')->nullOnDelete();

        });
    }

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