<?php

namespace Database\Seeders;

use App\Models\Currency;
use App\Models\Pricing;
use App\Models\Product;
use App\Models\ProductConfigOption;
use App\Models\ProductConfigOptionValue;
use App\Models\Tld;
use Illuminate\Database\Seeder;
use Illuminate\Support\Collection;

class PricingSeeder extends Seeder
{
    public function run(): void
    {
        // QA fixtures (PRC-532): tüm retail fiyatlandırma tek noktada.
        Pricing::whereIn('type', ['product', 'product_config', 'domain'])->delete();

        $cur = Currency::whereIn('code', ['TRY', 'USD', 'EUR'])->pluck('id', 'code');

        $this->seedProductPricing($cur);
        $this->seedConfigPricing($cur);
        $this->seedDomainPricing($cur);
    }

    private function seedProductPricing(Collection $cur): void
    {
        // slug => [currency => aylık fiyat]. Eksik currency = kasıtlı GAP.
        $monthly = [
            'qa-basic' => ['TRY' => 100, 'USD' => 10, 'EUR' => 9],
            'qa-pro'   => ['TRY' => 200, 'USD' => 20, 'EUR' => 18],
            'qa-cloud' => ['TRY' => 300, 'USD' => 30, 'EUR' => 27],
            'qa-free'  => ['TRY' => 0,   'USD' => 0,  'EUR' => 0],
            'qa-ssl'   => ['TRY' => 150, 'USD' => 15],               // EUR GAP
            'qa-soft'  => ['TRY' => 250, 'USD' => 25, 'EUR' => 22],
            'qa-gap'   => ['TRY' => 120],                            // USD + EUR GAP
        ];
        $annualSkip = ['qa-basic' => ['EUR']];
        $setup = ['qa-soft' => ['TRY' => 100, 'USD' => 10, 'EUR' => 9]];

        foreach ($monthly as $slug => $prices) {
            $product = Product::where('slug', $slug)->first();
            if (! $product) {
                continue;
            }
            foreach (['TRY', 'USD', 'EUR'] as $code) {
                if (! isset($prices[$code])) {
                    continue; // GAP
                }
                Pricing::create([
                    'type'         => 'product',
                    'relation_id'  => $product->id,
                    'segment'      => 'recurring',
                    'period'       => 1,
                    'time'         => 'month',
                    'amount'       => $prices[$code],
                    'setup_amount' => $setup[$slug][$code] ?? null,
                    'currency_id'  => $cur[$code],
                    'order'        => 1,
                ]);
                if (in_array($code, $annualSkip[$slug] ?? [], true)) {
                    continue; // yıllık×currency GAP
                }
                Pricing::create([
                    'type'         => 'product',
                    'relation_id'  => $product->id,
                    'segment'      => 'recurring',
                    'period'       => 12,
                    'time'         => 'month',
                    'amount'       => $prices[$code] * 10,
                    'setup_amount' => $setup[$slug][$code] ?? null,
                    'currency_id'  => $cur[$code],
                    'order'        => 12,
                ]);
            }
        }
    }

    private function seedConfigPricing(Collection $cur): void
    {
        $config = [
            'RAM' => [
                '2 GB' => ['TRY' => 0,   'USD' => 0,  'EUR' => 0],
                '4 GB' => ['TRY' => 50,  'USD' => 5,  'EUR' => 5],
                '8 GB' => ['TRY' => 100, 'USD' => 10, 'EUR' => 10],
            ],
            'Disk' => [
                '50 GB'  => ['TRY' => 0,  'USD' => 0, 'EUR' => 0],
                '100 GB' => ['TRY' => 20, 'EUR' => 2],               // USD GAP
            ],
            'Yedekleme' => [
                'Yok'      => ['TRY' => 0,  'USD' => 0, 'EUR' => 0],
                'Günlük'   => ['TRY' => 40, 'USD' => 4, 'EUR' => 4],
                'Haftalık' => ['TRY' => 20, 'USD' => 2, 'EUR' => 2],
            ],
            'vCPU' => [
                '1 Çekirdek' => ['TRY' => 0,   'USD' => 0,  'EUR' => 0],
                '2 Çekirdek' => ['TRY' => 60,  'USD' => 6,  'EUR' => 6],
                '4 Çekirdek' => ['TRY' => 120, 'USD' => 12, 'EUR' => 12],
            ],
        ];

        foreach ($config as $optionName => $values) {
            $option = ProductConfigOption::where('name', $optionName)->first();
            if (! $option) {
                continue;
            }
            foreach ($values as $valueStr => $prices) {
                $value = ProductConfigOptionValue::where('option_id', $option->id)
                    ->where('value', $valueStr)->first();
                if (! $value) {
                    continue;
                }
                foreach (['TRY', 'USD', 'EUR'] as $code) {
                    if (! isset($prices[$code])) {
                        continue; // GAP
                    }
                    foreach ([1 => 1, 12 => 10] as $period => $mult) {
                        Pricing::create([
                            'type'         => 'product_config',
                            'relation_id'  => $value->id,
                            'segment'      => 'monthly',
                            'period'       => $period,
                            'time'         => 'month',
                            'amount'       => $prices[$code] * $mult,
                            'setup_amount' => 0,
                            'currency_id'  => $cur[$code],
                            'order'        => $period,
                        ]);
                    }
                }
            }
        }
    }

    private function seedDomainPricing(Collection $cur): void
    {
        $tldPrices = [
            '.com'    => ['TRY' => 100, 'USD' => 10, 'EUR' => 9],
            '.net'    => ['TRY' => 120, 'USD' => 12, 'EUR' => 11],
            '.org'    => ['TRY' => 110, 'USD' => 11, 'EUR' => 10],
            '.io'     => ['TRY' => 200, 'USD' => 20, 'EUR' => 18],
            '.co'     => ['TRY' => 130, 'USD' => 13, 'EUR' => 12],
            '.dev'    => ['TRY' => 90,  'USD' => 9,  'EUR' => 8],
            '.xyz'    => ['TRY' => 60,  'EUR' => 5],                 // USD GAP
            '.vip'    => ['TRY' => 500, 'USD' => 50, 'EUR' => 45],
            '.com.tr' => ['TRY' => 150],                             // USD + EUR GAP
            '.net.tr' => ['TRY' => 150],                             // USD + EUR GAP
        ];

        foreach ($tldPrices as $tldStr => $prices) {
            $tld = Tld::where('tld', $tldStr)->first();
            if (! $tld) {
                continue;
            }
            foreach (['TRY', 'USD', 'EUR'] as $code) {
                if (! isset($prices[$code])) {
                    continue; // GAP
                }
                $base = $prices[$code];
                foreach (['register', 'renew'] as $segment) {
                    for ($year = 1; $year <= 3; $year++) {
                        Pricing::create([
                            'type'        => 'domain',
                            'relation_id' => $tld->id,
                            'segment'     => $segment,
                            'period'      => $year,
                            'time'        => 'year',
                            'amount'      => $base * $year,
                            'currency_id' => $cur[$code],
                            'order'       => $year,
                        ]);
                    }
                }
                Pricing::create([
                    'type'        => 'domain',
                    'relation_id' => $tld->id,
                    'segment'     => 'transfer',
                    'period'      => 1,
                    'time'        => 'year',
                    'amount'      => $base,
                    'currency_id' => $cur[$code],
                    'order'       => 1,
                ]);
            }
        }
    }
}
