<?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('phone_results', function (Blueprint $table) {
            $table->id();
            $table->foreignId('upload_id')->constrained('uploads')->cascadeOnDelete();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('original', 100);
            $table->string('e164', 20)->nullable();
            $table->string('local_format', 50)->nullable();
            $table->string('whatsapp_format', 20)->nullable();
            $table->string('dashed_format', 50)->nullable();
            $table->boolean('is_valid')->default(false);
            $table->boolean('is_duplicate')->default(false);
            $table->string('country_code', 4)->nullable();
            $table->string('country_name', 100)->nullable();
            $table->enum('number_type', ['mobile', 'landline', 'unknown'])->default('unknown');
            $table->tinyInteger('risk_score')->unsigned()->default(0)->comment('0-100');
            $table->boolean('whatsapp_likely')->default(false);
            $table->string('duplicate_cluster_id', 50)->nullable();
            $table->timestamps();

            $table->index(['upload_id', 'is_valid']);
            $table->index(['upload_id', 'is_duplicate']);
            $table->index(['tenant_id', 'country_code']);
            $table->index(['upload_id', 'number_type']);
        });
    }

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