<?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. Create 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' => '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ü',
                'status' => 'active',
                'notes' => 'Vito ve Sprinter araç filosuyla Urfa içi ve Gap turları transfer ortağı.',
            ],
            [
                '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',
                'status' => 'active',
                'notes' => 'Sıra geceleri ve yöresel Urfa yemekleri için grup restoranı.',
            ]
        ];

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

            // 2. Seed price lists and items for hotels
            if ($supplier->supplier_type === 'hotel') {
                $priceList = SupplierPriceList::create([
                    'agency_id' => $agency->id,
                    'supplier_id' => $supplier->id,
                    'name' => 'İlkbahar - Yaz Sezonu 2026',
                    'season_start' => '2026-01-01',
                    'season_end' => '2026-03-30',
                    'currency' => 'TRY',
                    'is_active' => true,
                    'notes' => 'Kdv ve Kahvaltı dahil net fiyatlardır.',
                ]);

                // Create dynamic room price items based on hotel company_name (from Excel screenshot!)
                if ($supplier->company_name === 'Dedeman Urfa') {
                    $items = [
                        ['room_type' => 'single', 'board' => 'BB', 'net' => 4400.00, 'sale' => 4800.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 1200.00],
                        ['room_type' => 'double', 'board' => 'BB', 'net' => 5775.00, 'sale' => 6400.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 1200.00],
                        ['room_type' => 'triple', 'board' => 'BB', 'net' => 7975.00, 'sale' => 8800.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 1200.00],
                        ['room_type' => 'child_0_6', 'board' => 'BB', 'net' => 0.00, 'sale' => 0.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 0.00],
                        ['room_type' => 'child_0_12', 'board' => 'BB', 'net' => 1443.75, 'sale' => 1600.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 1200.00],
                    ];
                } elseif ($supplier->company_name === 'Hane Han Butik Otel') {
                    $items = [
                        ['room_type' => 'single', 'board' => 'BB', 'net' => 4000.00, 'sale' => 4500.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 1000.00],
                        ['room_type' => 'double', 'board' => 'BB', 'net' => 5000.00, 'sale' => 5600.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 1000.00],
                        ['room_type' => 'triple', 'board' => 'BB', 'net' => 6500.00, 'sale' => 7200.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 1000.00],
                        ['room_type' => 'child_0_6', 'board' => 'BB', 'net' => 0.00, 'sale' => 0.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 0.00],
                        ['room_type' => 'child_0_12', 'board' => 'BB', 'net' => 1250.00, 'sale' => 1400.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 1000.00],
                    ];
                } else { // Grand Urfa
                    $items = [
                        ['room_type' => 'single', 'board' => 'BB', 'net' => 3000.00, 'sale' => 3400.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 750.00],
                        ['room_type' => 'double', 'board' => 'BB', 'net' => 4000.00, 'sale' => 4500.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 750.00],
                        ['room_type' => 'triple', 'board' => 'BB', 'net' => 5000.00, 'sale' => 5600.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 750.00],
                        ['room_type' => 'child_0_6', 'board' => 'BB', 'net' => 0.00, 'sale' => 0.00, 'market' => 'hepsi', 'price_type' => 'fit', 'hb_diff' => 0.00],
                        ['room_type' => 'child_0_12', 'board' => 'BB', 'net' => 1000.00, 'sale' => 1100.00, 'market' => 'hepsi', 'price_type' => 'fit', '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']];
                    $priceTypeLabel = [
                        'fit' => 'FIT Net',
                        'grup' => 'GRUP Net',
                    ][$itm['price_type']];
                    
                    SupplierPriceItem::create([
                        'price_list_id' => $priceList->id,
                        'title' => "{$roomLabel} - {$itm['board']} (Yerli & Yabancı / {$priceTypeLabel})",
                        'net_price' => $itm['net'],
                        'sale_price' => $itm['sale'],
                        'tax_rate' => 20.00,
                        'metadata' => [
                            'room_type' => $itm['room_type'],
                            'board_type' => $itm['board'],
                            'market' => $itm['market'],
                            'price_type' => $itm['price_type'],
                            'hb_diff' => $itm['hb_diff'],
                        ]
                    ]);
                }
            }

            // 3. Create dummy activities
            SupplierActivity::create([
                'agency_id' => $agency->id,
                'supplier_id' => $supplier->id,
                'activity_type' => 'note',
                'subject' => 'İlk Tanıtım & Anlaşma',
                'description' => 'Tedarikçi ile ilk görüşme gerçekleştirildi ve komisyon oranları üzerinde anlaşma sağlandı.',
                '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 Alınması',
                'description' => '2026 İlkbahar-Yaz fiyat tablosu talep edildi, Excel dosyası teslim alındı ve sisteme girildi.',
                'status' => 'completed',
                'priority' => 'high',
                'activity_date' => now()->subDays(2),
                'performed_by' => $userId,
            ]);
        }

        $this->command->info('5 adet Tedarikçi, fiyat listeleri ve aktiviteleri başarıyla oluşturuldu!');
    }
}
