<?php

namespace Database\Seeders;

use App\Models\Supplier\Supplier;
use App\Models\Supplier\SupplierPriceList;
use App\Models\Supplier\SupplierPriceItem;
use App\Models\Supplier\SupplierActivity;
use App\Models\Agency\Agency;
use App\Models\User;
use Illuminate\Database\Seeder;

class SupplierSeeder extends Seeder
{
    public function run(): void
    {
        $agency = Agency::first();
        if (!$agency) return;

        $user = User::first();
        $userId = $user ? $user->id : null;

        // 1. Define 10 diverse suppliers
        $suppliers = [
            [
                'type' => 'corporate',
                'supplier_type' => 'hotel',
                'company_name' => 'Dedeman Urfa',
                'company_tax_number' => '9988776655',
                'company_tax_office' => 'Şanlıurfa',
                'responsible_person_name' => 'Halil Demir',
                'responsible_person_phone' => '05329998877',
                'responsible_person_email' => 'halil.demir@dedeman.com',
                'phone' => '04142223344',
                'emails' => ['info@dedemanurfa.com', 'rezervasyon@dedemanurfa.com'],
                'phones' => ['04142223344', '04142223345'],
                'address' => 'Atatürk Bulvarı No:120',
                'city' => 'Şanlıurfa',
                'district' => 'Haliliye',
                'country' => 'TR',
                'status' => 'active',
                'notes' => 'Şanlıurfa merkezde 4 yıldızlı tesis. FIT ve grup satışları için ideal ortak.',
            ],
            [
                'type' => 'corporate',
                'supplier_type' => 'hotel',
                'company_name' => 'Hane Han Butik Otel',
                'company_tax_number' => '1122334455',
                'company_tax_office' => 'Şanlıurfa',
                'responsible_person_name' => 'Zeynep Kaya',
                'responsible_person_phone' => '05338887766',
                'responsible_person_email' => 'zeynep.kaya@hanehan.com',
                'phone' => '04143334455',
                'emails' => ['info@hanehan.com'],
                'phones' => ['04143334455'],
                'address' => 'Balıklıgöl Cd. No:45',
                'city' => 'Şanlıurfa',
                'district' => 'Eyyübiye',
                'country' => 'TR',
                'status' => 'active',
                'notes' => 'Tarihi Urfa konağından restore edilmiş premium butik otel.',
            ],
            [
                'type' => 'corporate',
                'supplier_type' => 'hotel',
                'company_name' => 'Grand Urfa Otel',
                'company_tax_number' => '5566778899',
                'company_tax_office' => 'Şanlıurfa',
                'responsible_person_name' => 'Ömer Şahin',
                'responsible_person_phone' => '05447776655',
                'responsible_person_email' => 'omer@grandurfa.com',
                'phone' => '04144445566',
                'emails' => ['info@grandurfa.com'],
                'phones' => ['04144445566'],
                'address' => 'Emniyet Cd. No:80',
                'city' => 'Şanlıurfa',
                'district' => 'Haliliye',
                'country' => 'TR',
                'status' => 'active',
                'notes' => '3 yıldızlı, temiz ve geniş odalara sahip operasyonel otel.',
            ],
            [
                'type' => 'corporate',
                'supplier_type' => 'hotel',
                'company_name' => 'Hilton Garden Inn Şanlıurfa',
                'company_tax_number' => '2233445566',
                'company_tax_office' => 'Şanlıurfa',
                'responsible_person_name' => 'Ahmet Yılmaz',
                'responsible_person_phone' => '05321112233',
                'responsible_person_email' => 'ahmet.yilmaz@hilton.com',
                'phone' => '04143182000',
                'emails' => ['info.urfa@hilton.com'],
                'phones' => ['04143182000'],
                'address' => 'Karakoyun İş Merkezi Yanı',
                'city' => 'Şanlıurfa',
                'district' => 'Karaköprü',
                'country' => 'TR',
                'status' => 'active',
                'notes' => '5 yıldızlı zincir otel konsepti, havuzlu ve spor salonlu modern tesis.',
            ],
            [
                'type' => 'corporate',
                'supplier_type' => 'hotel',
                'company_name' => 'Nevali Hotel',
                'company_tax_number' => '3344556677',
                'company_tax_office' => 'Şanlıurfa',
                'responsible_person_name' => 'Fatma Aslan',
                'responsible_person_phone' => '05334445566',
                'responsible_person_email' => 'sales@nevalihotel.com',
                'phone' => '04143189000',
                'emails' => ['info@nevalihotel.com'],
                'phones' => ['04143189000'],
                'address' => 'Abide Kavşağı No:5',
                'city' => 'Şanlıurfa',
                'district' => 'Haliliye',
                'country' => 'TR',
                'status' => 'active',
                'notes' => 'Şehir merkezinde, lüks toplantı salonları ve geniş yatak kapasiteli premium iş oteli.',
            ],
            [
                'type' => 'corporate',
                'supplier_type' => 'transportation',
                'company_name' => 'Urfa Trans Taşımacılık',
                'company_tax_number' => '4455667788',
                'company_tax_office' => 'Şanlıurfa',
                'responsible_person_name' => 'Mahmut Çelik',
                'responsible_person_phone' => '05356665544',
                'phone' => '04145556677',
                'emails' => ['operasyon@urfatrans.com'],
                'address' => 'Otogar Karşısı No:10',
                'city' => 'Şanlıurfa',
                'district' => 'Karaköprü',
                'country' => 'TR',
                'status' => 'active',
                'notes' => 'Vito ve Sprinter araç filosuyla Urfa içi ve Gap turları transfer ortağı.',
            ],
            [
                'type' => 'corporate',
                'supplier_type' => 'transportation',
                'company_name' => 'Gap Turizm Taşımacılık',
                'company_tax_number' => '5566778811',
                'company_tax_office' => 'Şanlıurfa',
                'responsible_person_name' => 'Cemal Aksoy',
                'responsible_person_phone' => '05367778899',
                'phone' => '04146669988',
                'emails' => ['transfer@gapturizm.com'],
                'address' => 'Karaköprü Caddesi No:82',
                'city' => 'Şanlıurfa',
                'district' => 'Karaköprü',
                'country' => 'TR',
                'status' => 'active',
                'notes' => 'Geniş otobüs ve minibüs filosu, kültür turları için D2 belgeli araç tedarikçisi.',
            ],
            [
                'type' => 'corporate',
                'supplier_type' => 'restaurant',
                'company_name' => 'Tarihi Konak Sofrası',
                'company_tax_number' => '8899001122',
                'company_tax_office' => 'Şanlıurfa',
                'responsible_person_name' => 'Mehmet Usta',
                'phone' => '04146667788',
                'emails' => ['lezzet@konaksofrasi.com'],
                'address' => 'Sarayönü Cd. No:20',
                'city' => 'Şanlıurfa',
                'district' => 'Eyyübiye',
                'country' => 'TR',
                'status' => 'active',
                'notes' => 'Sıra geceleri ve yöresel Urfa yemekleri için grup restoranı.',
            ],
            [
                'type' => 'corporate',
                'supplier_type' => 'restaurant',
                'company_name' => 'Balıklıgöl Restoran',
                'company_tax_number' => '9900112233',
                'company_tax_office' => 'Şanlıurfa',
                'responsible_person_name' => 'Kadir Bey',
                'phone' => '04143132020',
                'emails' => ['bilgi@balikligolrestoran.com'],
                'address' => 'Balıklıgöl Kenarı No:2',
                'city' => 'Şanlıurfa',
                'district' => 'Eyyübiye',
                'country' => 'TR',
                'status' => 'active',
                'notes' => 'Tarihi atmosferde, yerli turist gruplarına özel fiks menü hizmeti.',
            ],
            [
                'type' => 'corporate',
                'supplier_type' => 'visa',
                'company_name' => 'Anadolu Vize Danışmanlık',
                'company_tax_number' => '7788990011',
                'company_tax_office' => 'Şanlıurfa',
                'responsible_person_name' => 'Sibel Şen',
                'responsible_person_phone' => '05432223344',
                'phone' => '04143154422',
                'emails' => ['vize@anadolukongre.com'],
                'address' => 'Valilik Karşısı, Saray İş Hanı Kat:2',
                'city' => 'Şanlıurfa',
                'district' => 'Haliliye',
                'country' => 'TR',
                'status' => 'active',
                'notes' => 'Schengen, ABD ve İngiltere vize dosyası hazırlama ve başvuru takibi ortağı.',
            ]
        ];

        foreach ($suppliers as $supplierData) {
            $responsibleName = $supplierData['responsible_person_name'] ?? null;
            $responsiblePhone = $supplierData['responsible_person_phone'] ?? null;
            $responsibleEmail = $supplierData['responsible_person_email'] ?? null;

            unset(
                $supplierData['responsible_person_name'],
                $supplierData['responsible_person_phone'],
                $supplierData['responsible_person_email']
            );

            $supplier = Supplier::create(array_merge($supplierData, [
                'agency_id' => $agency->id,
            ]));

            if ($responsibleName) {
                $supplier->responsibles()->create([
                    'name' => $responsibleName,
                    'phone' => $responsiblePhone,
                    'email' => $responsibleEmail,
                    'position' => 'Sorumlu Yetkili',
                ]);
            }

            $mpTr = \App\Models\Agency\Marketplace::where('agency_id', $agency->id)->where('slug', 'yerli-pazar')->first();
            $mpEu = \App\Models\Agency\Marketplace::where('agency_id', $agency->id)->where('slug', 'avrupa-schengen')->first();

            $seedItemsForList = function($list, $currency, $priceModifier) use ($supplier) {
                if ($supplier->supplier_type === 'hotel') {
                    if ($supplier->company_name === 'Dedeman Urfa') {
                        $items = [
                            ['room_type' => 'single', 'board' => 'BB', 'net' => 4400.00, 'sale' => 4800.00, 'hb_diff' => 1200.00],
                            ['room_type' => 'double', 'board' => 'BB', 'net' => 5775.00, 'sale' => 6400.00, 'hb_diff' => 1200.00],
                            ['room_type' => 'triple', 'board' => 'BB', 'net' => 7975.00, 'sale' => 8800.00, 'hb_diff' => 1200.00],
                            ['room_type' => 'child_0_6', 'board' => 'BB', 'net' => 0.00, 'sale' => 0.00, 'hb_diff' => 0.00],
                            ['room_type' => 'child_0_12', 'board' => 'BB', 'net' => 1443.75, 'sale' => 1600.00, 'hb_diff' => 1200.00],
                        ];
                    } elseif ($supplier->company_name === 'Hane Han Butik Otel') {
                        $items = [
                            ['room_type' => 'single', 'board' => 'BB', 'net' => 4000.00, 'sale' => 4500.00, 'hb_diff' => 1000.00],
                            ['room_type' => 'double', 'board' => 'BB', 'net' => 5000.00, 'sale' => 5600.00, 'hb_diff' => 1000.00],
                            ['room_type' => 'triple', 'board' => 'BB', 'net' => 6500.00, 'sale' => 7200.00, 'hb_diff' => 1000.00],
                            ['room_type' => 'child_0_6', 'board' => 'BB', 'net' => 0.00, 'sale' => 0.00, 'hb_diff' => 0.00],
                            ['room_type' => 'child_0_12', 'board' => 'BB', 'net' => 1250.00, 'sale' => 1400.00, 'hb_diff' => 1000.00],
                        ];
                    } elseif ($supplier->company_name === 'Hilton Garden Inn Şanlıurfa') {
                        $items = [
                            ['room_type' => 'single', 'board' => 'BB', 'net' => 5200.00, 'sale' => 5800.00, 'hb_diff' => 1500.00],
                            ['room_type' => 'double', 'board' => 'BB', 'net' => 6800.00, 'sale' => 7500.00, 'hb_diff' => 1500.00],
                            ['room_type' => 'triple', 'board' => 'BB', 'net' => 9000.00, 'sale' => 10000.00, 'hb_diff' => 1500.00],
                            ['room_type' => 'child_0_6', 'board' => 'BB', 'net' => 0.00, 'sale' => 0.00, 'hb_diff' => 0.00],
                            ['room_type' => 'child_0_12', 'board' => 'BB', 'net' => 1800.00, 'sale' => 2000.00, 'hb_diff' => 1500.00],
                        ];
                    } elseif ($supplier->company_name === 'Nevali Hotel') {
                        $items = [
                            ['room_type' => 'single', 'board' => 'BB', 'net' => 4800.00, 'sale' => 5400.00, 'hb_diff' => 1300.00],
                            ['room_type' => 'double', 'board' => 'BB', 'net' => 6200.00, 'sale' => 6900.00, 'hb_diff' => 1300.00],
                            ['room_type' => 'triple', 'board' => 'BB', 'net' => 8500.00, 'sale' => 9400.00, 'hb_diff' => 1300.00],
                            ['room_type' => 'child_0_6', 'board' => 'BB', 'net' => 0.00, 'sale' => 0.00, 'hb_diff' => 0.00],
                            ['room_type' => 'child_0_12', 'board' => 'BB', 'net' => 1600.00, 'sale' => 1800.00, 'hb_diff' => 1300.00],
                        ];
                    } else { // Grand Urfa Otel
                        $items = [
                            ['room_type' => 'single', 'board' => 'BB', 'net' => 3000.00, 'sale' => 3400.00, 'hb_diff' => 750.00],
                            ['room_type' => 'double', 'board' => 'BB', 'net' => 4000.00, 'sale' => 4500.00, 'hb_diff' => 750.00],
                            ['room_type' => 'triple', 'board' => 'BB', 'net' => 5000.00, 'sale' => 5600.00, 'hb_diff' => 750.00],
                            ['room_type' => 'child_0_6', 'board' => 'BB', 'net' => 0.00, 'sale' => 0.00, 'hb_diff' => 0.00],
                            ['room_type' => 'child_0_12', 'board' => 'BB', 'net' => 1000.00, 'sale' => 1100.00, 'hb_diff' => 750.00],
                        ];
                    }

                    foreach ($items as $itm) {
                        $roomLabel = [
                            'single' => 'Single',
                            'double' => 'Double',
                            'triple' => 'Triple',
                            'child_0_6' => '0-6 Yaş',
                            'child_0_12' => '0-12 Yaş',
                        ][$itm['room_type']];
                        
                        SupplierPriceItem::create([
                            'price_list_id' => $list->id,
                            'title' => "{$roomLabel} Oda - {$itm['board']} (Grup & Münferit)",
                            'currency' => $currency,
                            'net_price' => round($itm['net'] * $priceModifier, 2),
                            'sale_price' => round($itm['sale'] * $priceModifier, 2),
                            'tax_rate' => 20.00,
                            'metadata' => [
                                'room_type' => $itm['room_type'],
                                'board_type' => $itm['board'],
                                'market' => 'hepsi',
                                'price_type' => 'fit',
                                'hb_diff' => round($itm['hb_diff'] * $priceModifier, 2),
                            ]
                        ]);
                    }
                } elseif ($supplier->supplier_type === 'transportation') {
                    $routes = [
                        ['route' => 'Şanlıurfa Havalimanı - Otel (Tek Yön)', 'vehicle' => 'Vito (1-6 Kişi)', 'net' => 1200.00, 'sale' => 1500.00],
                        ['route' => 'Şanlıurfa Havalimanı - Otel (Tek Yön)', 'vehicle' => 'Sprinter (1-16 Kişi)', 'net' => 2200.00, 'sale' => 2700.00],
                        ['route' => 'Şanlıurfa Merkez - Göbeklitepe (Gidiş-Dönüş)', 'vehicle' => 'Vito (1-6 Kişi)', 'net' => 2000.00, 'sale' => 2500.00],
                        ['route' => 'Şanlıurfa Merkez - Göbeklitepe (Gidiş-Dönüş)', 'vehicle' => 'Sprinter (1-16 Kişi)', 'net' => 3200.00, 'sale' => 3900.00],
                        ['route' => 'Şanlıurfa Merkez - Harran (Gidiş-Dönüş)', 'vehicle' => 'Sprinter (1-16 Kişi)', 'net' => 3500.00, 'sale' => 4200.00],
                    ];

                    foreach ($routes as $route) {
                        SupplierPriceItem::create([
                            'price_list_id' => $list->id,
                            'title' => "{$route['route']} - Araç: {$route['vehicle']}",
                            'currency' => $currency,
                            'net_price' => round($route['net'] * $priceModifier, 2),
                            'sale_price' => round($route['sale'] * $priceModifier, 2),
                            'tax_rate' => 20.00,
                            'metadata' => [
                                'route' => $route['route'],
                                'vehicle_type' => $route['vehicle'],
                            ]
                        ]);
                    }
                } elseif ($supplier->supplier_type === 'restaurant') {
                    $menus = [
                        ['menu' => 'Standart Urfa Kebap Menüsü', 'details' => 'Meze + Kebap + İçecek + Tatlı', 'net' => 380.00, 'sale' => 480.00],
                        ['menu' => 'Yöresel Sıra Gecesi Fiks Menü', 'details' => 'Çorba + Meze + Karışık Kebap + Bostana + Şıllık Tatlısı', 'net' => 850.00, 'sale' => 1100.00],
                        ['menu' => 'Geleneksel Urfa Ev Kahvaltısı', 'details' => 'Yöresel kahvaltılıklar + Kaymak + Bal + Çay', 'net' => 250.00, 'sale' => 320.00],
                    ];

                    foreach ($menus as $menu) {
                        SupplierPriceItem::create([
                            'price_list_id' => $list->id,
                            'title' => "{$menu['menu']} ({$menu['details']})",
                            'currency' => $currency,
                            'net_price' => round($menu['net'] * $priceModifier, 2),
                            'sale_price' => round($menu['sale'] * $priceModifier, 2),
                            'tax_rate' => 10.00,
                            'metadata' => [
                                'menu_name' => $menu['menu'],
                                'menu_type' => 'group',
                            ]
                        ]);
                    }
                } else { // Visa or other services
                    $services = [
                        ['service' => 'Schengen Vize Danışmanlık ve Dosya Hazırlama Bedeli', 'net' => 1500.00, 'sale' => 2000.00],
                        ['service' => 'İngiltere Vize Başvuru Danışmanlık Bedeli', 'net' => 1800.00, 'sale' => 2400.00],
                        ['service' => 'Amerika Vize Dosyası Hazırlama Hizmeti', 'net' => 2000.00, 'sale' => 2600.00],
                    ];

                    foreach ($services as $srv) {
                        SupplierPriceItem::create([
                            'price_list_id' => $list->id,
                            'title' => $srv['service'],
                            'currency' => $currency,
                            'net_price' => round($srv['net'] * $priceModifier, 2),
                            'sale_price' => round($srv['sale'] * $priceModifier, 2),
                            'tax_rate' => 20.00,
                            'metadata' => [
                                'service_name' => $srv['service'],
                            ]
                        ]);
                    }
                }
            };

            if ($mpTr) {
                $priceListTr = SupplierPriceList::create([
                    'agency_id' => $agency->id,
                    'supplier_id' => $supplier->id,
                    'name' => '2026 Sezon Fiyat Tarifesi (TR)',
                    'season_start' => '2026-01-01',
                    'season_end' => '2026-12-31',
                    'marketplace_id' => $mpTr->id,
                    'is_active' => true,
                    'notes' => 'Kdv ve tüm vergiler dahil net fiyat sözleşmesidir.',
                ]);
                $seedItemsForList($priceListTr, 'TRY', 1.0);
            }

            if ($mpEu) {
                $priceListEu = SupplierPriceList::create([
                    'agency_id' => $agency->id,
                    'supplier_id' => $supplier->id,
                    'name' => '2026 Sezon Fiyat Tarifesi (EU)',
                    'season_start' => '2026-01-01',
                    'season_end' => '2026-12-31',
                    'marketplace_id' => $mpEu->id,
                    'is_active' => true,
                    'notes' => 'Euro bazlı Avrupa pazarı fiyat sözleşmesidir.',
                ]);
                $seedItemsForList($priceListEu, 'EUR', 1.0 / 35.0);
            }

            // 3. Create activity history
            SupplierActivity::create([
                'agency_id' => $agency->id,
                'supplier_id' => $supplier->id,
                'activity_type' => 'note',
                'subject' => 'Sözleşme & Tedarikçi Tanıtımı',
                'description' => 'Tedarikçi operasyonel şartları ve komisyon yapısı görüşülerek sisteme kaydedildi.',
                'status' => 'completed',
                'priority' => 'normal',
                'activity_date' => now()->subDays(5),
                'performed_by' => $userId,
            ]);

            SupplierActivity::create([
                'agency_id' => $agency->id,
                'supplier_id' => $supplier->id,
                'activity_type' => 'call',
                'subject' => 'Sezon Fiyatlarının Onaylanması',
                'description' => '2026 sezon fiyat tarifesi karşılıklı olarak onaylandı ve seeder verisi olarak sisteme işlendi.',
                'status' => 'completed',
                'priority' => 'high',
                'activity_date' => now()->subDays(2),
                'performed_by' => $userId,
            ]);
        }
    }
}
