<?php

namespace Database\Seeders;

use App\Models\Agency\Agency;
use App\Models\Agency\Currency;
use App\Models\Agency\Marketplace;
use App\Models\Definition\Language;
use App\Models\Product\Product;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class MarketplaceSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $agencies = Agency::all();

        foreach ($agencies as $agency) {
            // Fetch available languages in system
            $langTr = Language::whereIn('code', ['TR', 'tr'])->first();
            $langEn = Language::whereIn('code', ['EN', 'en'])->first();
            $langDe = Language::whereIn('code', ['DE', 'de'])->first();
            $langRu = Language::whereIn('code', ['RU', 'ru'])->first();
            $langFr = Language::whereIn('code', ['FR', 'fr'])->first();
            $langNl = Language::whereIn('code', ['NL', 'nl'])->first();

            // Default fallback if some languages are missing
            $allLangs = Language::all();
            $defaultLang = $allLangs->first();

            // Fetch agency specific currencies
            $currTry = Currency::where('agency_id', $agency->id)->where('code', 'TRY')->first();
            $currUsd = Currency::where('agency_id', $agency->id)->where('code', 'USD')->first();
            $currEur = Currency::where('agency_id', $agency->id)->where('code', 'EUR')->first();
            $currGbp = Currency::where('agency_id', $agency->id)->where('code', 'GBP')->first();

            $allCurrs = Currency::where('agency_id', $agency->id)->get();
            $defaultCurr = $allCurrs->first();

            // 1. Türkiye Pazarı (TR Market)
            $mpTr = Marketplace::updateOrCreate(
                [
                    'agency_id' => $agency->id,
                    'slug' => 'turkiye-pazari',
                ],
                [
                    'name' => 'Türkiye Pazarı',
                    'default_language_id' => $langTr ? $langTr->id : ($defaultLang ? $defaultLang->id : null),
                    'default_currency_id' => $currTry ? $currTry->id : ($defaultCurr ? $defaultCurr->id : null),
                    'is_active' => true,
                ]
            );

            // Sync languages for TR Market (TR, EN, DE, RU)
            $trLangs = collect([$langTr, $langEn, $langDe, $langRu])->filter()->pluck('id')->toArray();
            if (empty($trLangs) && $defaultLang) {
                $trLangs = [$defaultLang->id];
            }
            $mpTr->languages()->sync($trLangs);

            // Sync currencies for TR Market (TRY, USD, EUR)
            $trCurrs = collect([$currTry, $currUsd, $currEur])->filter()->pluck('id')->toArray();
            if (empty($trCurrs) && $defaultCurr) {
                $trCurrs = [$defaultCurr->id];
            }
            $mpTr->currencies()->sync($trCurrs);

            // 2. Avrupa Pazarı (EU Market)
            $mpEu = Marketplace::updateOrCreate(
                [
                    'agency_id' => $agency->id,
                    'slug' => 'avrupa-pazari',
                ],
                [
                    'name' => 'Avrupa Pazarı',
                    'default_language_id' => $langEn ? $langEn->id : ($defaultLang ? $defaultLang->id : null),
                    'default_currency_id' => $currEur ? $currEur->id : ($defaultCurr ? $defaultCurr->id : null),
                    'is_active' => true,
                ]
            );

            // Sync languages for EU Market (EN, DE, FR, NL)
            $euLangs = collect([$langEn, $langDe, $langFr, $langNl])->filter()->pluck('id')->toArray();
            if (empty($euLangs) && $defaultLang) {
                $euLangs = [$defaultLang->id];
            }
            $mpEu->languages()->sync($euLangs);

            // Sync currencies for EU Market (EUR, USD, GBP)
            $euCurrs = collect([$currEur, $currUsd, $currGbp])->filter()->pluck('id')->toArray();
            if (empty($euCurrs) && $defaultCurr) {
                $euCurrs = [$defaultCurr->id];
            }
            $mpEu->currencies()->sync($euCurrs);

            // Sync all agency products to these marketplaces by default to make them functional
            $products = Product::where('agency_id', $agency->id)->get();
            if ($products->isNotEmpty()) {
                $productIds = $products->pluck('id')->toArray();
                $mpTr->products()->sync($productIds);
                $mpEu->products()->sync($productIds);
            }
        }
    }
}
