<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * "Sizin için bulalım" talepleri: kullanıcı aradığı mülkü tarif eder,
     * firmalar Agency Panel'de görür ve iletişime geçer.
     * company_id NULL = tüm firmalara açık (genel) talep.
     */
    public function up(): void
    {
        Schema::create('property_requests', function (Blueprint $table) {
            $table->id();

            $table->foreignId('user_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();

            $table->foreignId('company_id')
                ->nullable()
                ->constrained('companies')
                ->cascadeOnDelete();

            $table->foreignId('category_id')
                ->nullable()
                ->constrained('listing_categories')
                ->nullOnDelete();
            $table->string('category_name', 160)->nullable();

            $table->string('location', 160)->nullable();
            $table->decimal('price_min', 15, 2)->nullable();
            $table->decimal('price_max', 15, 2)->nullable();
            $table->string('currency', 3)->nullable();

            $table->text('description');

            $table->string('name');
            $table->string('email');
            $table->string('phone', 32);
            $table->string('locale', 5)->nullable();
            $table->string('source', 40)->default('home_cta');

            $table->string('status', 20)->default('new'); // new | contacted | closed
            $table->timestamp('contacted_at')->nullable();

            $table->timestamps();

            $table->index(['company_id', 'status', 'created_at']);
        });
    }

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