<?php

namespace Database\Seeders;

use App\Models\Currency;
use App\Models\DomainCost;
use App\Models\Pricing;
use App\Models\Tld;
use Illuminate\Database\Seeder;

class DomainCostSeeder extends Seeder
{
    public function run(): void
    {
        // QA fixtures (PRC-532): TLD maliyetleri = register TRY × 0.5.
        Pricing::where('type', 'domain_cost')->delete();
        DomainCost::query()->forceDelete();

        $tryId = Currency::where('code', 'TRY')->value('id');
        if ($tryId === null) {
            throw new \RuntimeException('DomainCostSeeder: TRY para birimi bulunamadı (CurrencySeeder çalıştı mı?)');
        }

        $costs = [
            '.com' => 50, '.net' => 60, '.org' => 55, '.io' => 100, '.co' => 65,
            '.dev' => 45, '.xyz' => 30, '.vip' => 250, '.com.tr' => 75, '.net.tr' => 75,
        ];

        foreach ($costs as $tldStr => $amount) {
            $tld = Tld::where('tld', $tldStr)->first();
            if (! $tld) {
                continue;
            }
            $dc = DomainCost::create([
                'tld_id'     => $tld->id,
                'registrar'  => 'dummyregistrar',
                'is_active'  => true,
                'metadata'   => ['description' => $tldStr.' QA fixture cost'],
            ]);
            Pricing::create([
                'type'        => 'domain_cost',
                'relation_id' => $dc->id,
                'segment'     => 'cost',
                'period'      => 1,
                'time'        => 'year',
                'amount'      => $amount,
                'currency_id' => $tryId,
                'order'       => 1,
            ]);
        }
    }
}
