<?php

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

/**
 * PRC-1113 — Migrate-from-WHMCS addon izole şeması (Faz-1 per-addon migration kuralı).
 * Ledger idempotency+resumable+dry-run'ı; eşleme tabloları admin manuel mapping'i tutar.
 */
return new class extends Migration
{
    public function up(): void
    {
        // Ledger: her WHMCS kaydının procp karşılığı. UNIQUE(source_type, source_id)
        // → re-run zaten-import edileni atlar (idempotent + resumable).
        Schema::create('whmcs_migration_map', function (Blueprint $table) {
            $table->id();
            $table->string('source_type', 40);   // client|domain|service|invoice|order|ticket
            $table->unsignedBigInteger('source_id');
            $table->string('target_type', 40);
            $table->unsignedBigInteger('target_id');
            $table->string('run_id', 40)->nullable();
            $table->timestamp('created_at')->nullable();

            $table->unique(['source_type', 'source_id']);
            $table->index(['target_type', 'target_id']);
        });

        // Ürün eşleme: WHMCS tblproducts → procp Product (+ module). Admin doldurur.
        Schema::create('whmcs_product_map', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('whmcs_product_id')->unique();
            $table->unsignedBigInteger('procp_product_id')->nullable();
            $table->string('procp_module')->nullable();
            $table->timestamps();
        });

        // Domain TLD eşleme: WHMCS TLD ('.com') → procp Tld + registrar. Admin doldurur.
        Schema::create('whmcs_tld_map', function (Blueprint $table) {
            $table->id();
            $table->string('whmcs_tld')->unique();
            $table->unsignedBigInteger('procp_tld_id')->nullable();
            $table->string('procp_registrar')->nullable();
            $table->timestamps();
        });

        // Currency eşleme: WHMCS tblcurrencies.id → procp Currency.id. Admin doldurur.
        Schema::create('whmcs_currency_map', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('whmcs_currency_id')->unique();
            $table->unsignedBigInteger('procp_currency_id')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('whmcs_currency_map');
        Schema::dropIfExists('whmcs_tld_map');
        Schema::dropIfExists('whmcs_product_map');
        Schema::dropIfExists('whmcs_migration_map');
    }
};
