<?php

declare(strict_types=1);

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

return new class extends Migration {
    public function up(): void
    {
        if (!Schema::hasTable('properties')) {
            Schema::create('properties', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('hashkey', 300)->unique();
                
                $table->string('name')->index();
                $table->string('location')->nullable();
                $table->decimal('price', 15, 2)->default(0);
                $table->string('status')->default('available')->index();
                $table->json('details')->nullable();
                
                $table->boolean('is_active')->default(true);
                $table->unsignedBigInteger('created_by')->nullable();
                $table->unsignedBigInteger('updated_by')->nullable();
                
                $table->foreign('created_by')->references('id')->on('users')->nullOnDelete();
                $table->foreign('updated_by')->references('id')->on('users')->nullOnDelete();
                
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('referral_keys')) {
            Schema::create('referral_keys', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('hashkey', 300)->unique();
                
                $table->unsignedBigInteger('user_id')->index();
                $table->string('key')->unique()->index();
                
                $table->boolean('is_active')->default(true);
                $table->unsignedBigInteger('created_by')->nullable();
                $table->unsignedBigInteger('updated_by')->nullable();
                
                $table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();
                $table->foreign('created_by')->references('id')->on('users')->nullOnDelete();
                $table->foreign('updated_by')->references('id')->on('users')->nullOnDelete();
                
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('referrals')) {
            Schema::create('referrals', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('hashkey', 300)->unique();
                
                $table->unsignedBigInteger('property_id')->index();
                $table->unsignedBigInteger('referrer_id')->index();
                $table->unsignedBigInteger('referred_id')->nullable()->index(); // Can be null if it's just a lead
                $table->string('referred_name')->nullable();
                $table->string('referred_contact')->nullable();
                
                $table->string('status')->default('pending')->index();
                $table->json('details')->nullable();
                
                $table->boolean('is_active')->default(true);
                $table->unsignedBigInteger('created_by')->nullable();
                $table->unsignedBigInteger('updated_by')->nullable();
                
                $table->foreign('property_id')->references('id')->on('properties')->cascadeOnDelete();
                $table->foreign('referrer_id')->references('id')->on('users')->cascadeOnDelete();
                $table->foreign('referred_id')->references('id')->on('users')->nullOnDelete();
                $table->foreign('created_by')->references('id')->on('users')->nullOnDelete();
                $table->foreign('updated_by')->references('id')->on('users')->nullOnDelete();
                
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('referrals');
        Schema::dropIfExists('referral_keys');
        Schema::dropIfExists('properties');
    }
};
