<?php

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

/**
 * Pazarlama sitesindeki "Online Basvuru Formu" ile gelen bireysel kullanici basvurulari (CRM lead).
 * Yonetim panelinde bir bayiye atanip subscriptions kaydina donusturulebilir.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('applications', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('phone', 20);
            $table->string('email')->nullable();
            $table->text('address')->nullable();
            $table->string('il', 100);
            $table->string('ilce', 100);
            $table->foreignId('package_id')->constrained('packages')->restrictOnDelete();

            $table->enum('status', ['new', 'assigned', 'contacted', 'converted', 'rejected'])->default('new')
                ->comment('new=Yeni, assigned=Atandi, contacted=Iletisime Gecildi, converted=Aboneliğe Donustu, rejected=Reddedildi');

            $table->foreignId('assigned_dealer_id')->nullable()->constrained('dealers')->nullOnDelete();
            $table->foreignId('subscription_id')->nullable()->constrained('subscriptions')->nullOnDelete();

            $table->string('source')->default('website')->comment('website, admin, api');
            $table->string('ip_address', 45)->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->index(['status']);
            $table->index(['il', 'ilce']);
        });
    }

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