<?php

namespace Database\Seeders;

use App\Models\Customer\Customer;
use App\Models\Agency\Agency;
use Illuminate\Database\Seeder;

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

          // ─── ŞİRKET KARTLARI (customer_type = corporate) ───────────────────────
          // Şirketler artık ayrı bir tablo değil; kurumsal tipte birer müşteri kartıdır.
          $teknoloji = Customer::create([
               'agency_id' => $agency->id,
               'customer_type' => 'corporate',
               'company_name' => 'Teknoloji A.Ş.',
               'company_type' => 'joint_stock_company',
               'company_tax_number' => '1234567890',
               'company_tax_office' => 'Boğaziçi',
               'status' => 'active',
               'priority' => true,
               'customer_segment' => 'vip',
               'billing_address' => 'Levent Plaza No:45 Kat:12',
               'billing_city' => 'İstanbul',
               'billing_district' => 'Beşiktaş',
               'billing_tax_number' => '1234567890',
               'billing_tax_office' => 'Boğaziçi',
               'billing_email' => 'fatura@teknoloji.as',
          ]);

          $pazarlama = Customer::create([
               'agency_id' => $agency->id,
               'customer_type' => 'corporate',
               'company_name' => 'Pazarlama Ltd. Şti.',
               'company_type' => 'limited_company',
               'company_tax_number' => '9876543210',
               'company_tax_office' => 'Mecidiyeköy',
               'status' => 'active',
               'priority' => true,
               'customer_segment' => 'vip',
               'billing_address' => 'Büyükdere Caddesi No:88 Kat:4',
               'billing_city' => 'İstanbul',
               'billing_district' => 'Şişli',
               'billing_tax_number' => '9876543210',
               'billing_tax_office' => 'Mecidiyeköy',
               'billing_email' => 'accounting@pazarlama.com',
          ]);

          // ─── KİŞİLER (customer_type = individual) ───────────────────────────────
          // 'companies' anahtarı, kişinin bağlanacağı şirket kart(lar)ını belirtir (pivot).
          $customers = [
               [
                    'first_name' => 'Ahmet',
                    'last_name' => 'Yılmaz',
                    'emails' => ['ahmet.yilmaz@gmail.com', 'ahmet.is@sirket.com'],
                    'phone' => '05321112233',
                    'birth_date' => '1985-05-15',
                    'gender' => 'male',
                    'national_id' => '12345678901',
                    'nationality' => 'TR',
                    'city' => 'İstanbul',
                    'district' => 'Beşiktaş',
                    'customer_type' => 'individual',
                    'status' => 'active',
                    'priority' => true,
                    'customer_segment' => 'vip',
                    'billing_address' => 'Beşiktaş Mh. Atatürk Cd. No:12 D:4',
                    'billing_email' => 'ahmet.yilmaz@gmail.com',
                    'billing_tax_number' => '12345678901',
               ],
               [
                    'first_name' => 'Ayşe',
                    'last_name' => 'Demir',
                    'emails' => ['ayse.demir@hotmail.com'],
                    'phone' => '05332223344',
                    'birth_date' => '1990-08-20',
                    'gender' => 'female',
                    'national_id' => '23456789012',
                    'nationality' => 'TR',
                    'city' => 'Ankara',
                    'district' => 'Çankaya',
                    'customer_type' => 'individual',
                    'status' => 'active',
                    'priority' => false,
                    'customer_segment' => 'regular',
                    'billing_address' => 'Çankaya Mh. Kızılay Cd. No:5 D:8',
                    'billing_email' => 'ayse.demir@hotmail.com',
                    'billing_tax_number' => '23456789012',
               ],
               [
                    'first_name' => 'Mehmet',
                    'last_name' => 'Kaya',
                    'emails' => ['mehmet.kaya@outlook.com'],
                    'phone' => '05443334455',
                    'birth_date' => '1978-03-10',
                    'gender' => 'male',
                    'nationality' => 'TR',
                    'city' => 'İzmir',
                    'district' => 'Konak',
                    'customer_type' => 'individual',
                    'status' => 'active',
                    'priority' => false,
                    'customer_segment' => 'occasional',
                    'billing_address' => 'Konak Mh. Alsancak Sk. No:45',
                    'billing_email' => 'mehmet.kaya@outlook.com',
                    'billing_tax_number' => '34567890123',
               ],
               [
                    'first_name' => 'Fatma',
                    'last_name' => 'Şahin',
                    'emails' => ['fatma.sahin@gmail.com'],
                    'phone' => '05554445566',
                    'birth_date' => '1995-12-05',
                    'gender' => 'female',
                    'nationality' => 'TR',
                    'city' => 'Antalya',
                    'district' => 'Muratpaşa',
                    'customer_type' => 'individual',
                    'status' => 'active',
                    'priority' => true,
                    'customer_segment' => 'frequent',
                    'billing_address' => 'Muratpaşa Mh. Lara Cd. No:78',
                    'billing_email' => 'fatma.sahin@gmail.com',
                    'billing_tax_number' => '45678901234',
               ],
               // ─── Teknoloji A.Ş.'ye bağlı kişiler ───
               [
                    'first_name' => 'Can',
                    'last_name' => 'Öztürk',
                    'emails' => ['can.ozturk@company.com', 'can.ozturk@gmail.com', 'bilgi@teknoloji.as'],
                    'phone' => '02123334455',
                    'customer_type' => 'individual',
                    'company_position' => 'Satın Alma Müdürü',
                    'companies' => [$teknoloji->id],
                    'billing_address' => 'Levent Plaza No:45 Kat:12',
                    'billing_city' => 'İstanbul',
                    'billing_district' => 'Beşiktaş',
                    'billing_tax_number' => '12345678999',
                    'billing_email' => 'can.ozturk@company.com',
                    'status' => 'active',
                    'priority' => true,
                    'customer_segment' => 'vip',
               ],
               [
                    'first_name' => 'Cem',
                    'last_name' => 'Yılmaz',
                    'emails' => ['cem.yilmaz@company.com'],
                    'phone' => '02123334466',
                    'customer_type' => 'individual',
                    'company_position' => 'IT Müdürü',
                    'companies' => [$teknoloji->id],
                    'billing_address' => 'Levent Plaza No:45 Kat:12',
                    'billing_city' => 'İstanbul',
                    'billing_district' => 'Beşiktaş',
                    'billing_tax_number' => '12345678988',
                    'billing_email' => 'cem.yilmaz@company.com',
                    'status' => 'active',
                    'priority' => false,
                    'customer_segment' => 'regular',
               ],
               // ─── Pazarlama Ltd. Şti.'ye bağlı kişiler ───
               [
                    'first_name' => 'Merve',
                    'last_name' => 'Aksu',
                    'emails' => ['merve.aksu@pazarlama.com'],
                    'phone' => '02124445566',
                    'customer_type' => 'individual',
                    'company_position' => 'Pazarlama Müdürü',
                    'companies' => [$pazarlama->id],
                    'billing_address' => 'Büyükdere Caddesi No:88 Kat:4',
                    'billing_city' => 'İstanbul',
                    'billing_district' => 'Şişli',
                    'billing_tax_number' => '98765432199',
                    'billing_email' => 'merve.aksu@pazarlama.com',
                    'status' => 'active',
                    'priority' => true,
                    'customer_segment' => 'vip',
               ],
               [
                    'first_name' => 'Hakan',
                    'last_name' => 'Demir',
                    'emails' => ['hakan.demir@pazarlama.com'],
                    'phone' => '02124445577',
                    'customer_type' => 'individual',
                    'company_position' => 'Satış Temsilcisi',
                    // Hakan hem Pazarlama'ya hem Teknoloji'ye danışmanlık veriyor (çoklu şirket örneği)
                    'companies' => [$pazarlama->id, $teknoloji->id],
                    'billing_address' => 'Büyükdere Caddesi No:88 Kat:4',
                    'billing_city' => 'İstanbul',
                    'billing_district' => 'Şişli',
                    'billing_tax_number' => '98765432188',
                    'billing_email' => 'hakan.demir@pazarlama.com',
                    'status' => 'active',
                    'priority' => false,
                    'customer_segment' => 'regular',
               ],
               [
                    'first_name' => 'John',
                    'last_name' => 'Doe',
                    'emails' => ['john.doe@yahoo.com', 'j.doe@icloud.com'],
                    'phone' => '+447711223344',
                    'birth_date' => '1982-01-25',
                    'gender' => 'male',
                    'passport_number' => 'G1234567',
                    'nationality' => 'GB',
                    'city' => 'London',
                    'country' => 'GB',
                    'customer_type' => 'individual',
                    'status' => 'active',
                    'priority' => false,
                    'customer_segment' => 'regular',
                    'billing_address' => 'Baker Street 221B, London',
                    'billing_email' => 'john.doe@yahoo.com',
                    'billing_tax_number' => '99999999999',
               ],
               [
                    'first_name' => 'Elif',
                    'last_name' => 'Aydın',
                    'emails' => ['elif.aydin@gmail.com'],
                    'phone' => '05366667788',
                    'birth_date' => '1988-06-30',
                    'gender' => 'female',
                    'nationality' => 'TR',
                    'city' => 'Bursa',
                    'district' => 'Nilüfer',
                    'customer_type' => 'individual',
                    'status' => 'inactive',
                    'priority' => false,
                    'customer_segment' => 'occasional',
                    'billing_address' => 'Nilüfer Mh. FSM Blv. No:8',
                    'billing_email' => 'elif.aydin@gmail.com',
                    'billing_tax_number' => '56789012345',
               ],
               [
                    'first_name' => 'Burak',
                    'last_name' => 'Çelik',
                    'emails' => ['burak.celik@hotmail.com'],
                    'phone' => '05377778899',
                    'birth_date' => '1992-11-12',
                    'gender' => 'male',
                    'nationality' => 'TR',
                    'city' => 'Trabzon',
                    'district' => 'Ortahisar',
                    'customer_type' => 'individual',
                    'status' => 'active',
                    'priority' => false,
                    'customer_segment' => 'frequent',
                    'billing_address' => 'Ortahisar Mh. Karadeniz Cd. No:3',
                    'billing_email' => 'burak.celik@hotmail.com',
                    'billing_tax_number' => '67890123456',
               ],
               [
                    'first_name' => 'Selin',
                    'last_name' => 'Koç',
                    'emails' => ['selin.koc@gmail.com'],
                    'phone' => '05388889900',
                    'birth_date' => '1984-09-18',
                    'gender' => 'female',
                    'nationality' => 'TR',
                    'city' => 'Muğla',
                    'district' => 'Bodrum',
                    'customer_type' => 'individual',
                    'status' => 'active',
                    'priority' => true,
                    'customer_segment' => 'vip',
                    'billing_address' => 'Bodrum Mh. Marina Yolu No:14',
                    'billing_email' => 'selin.koc@gmail.com',
                    'billing_tax_number' => '78901234567',
               ],
               [
                    'first_name' => 'Ali',
                    'last_name' => 'Veli',
                    'emails' => ['ali.veli@test.com'],
                    'phone' => '05399990011',
                    'customer_type' => 'individual',
                    'status' => 'active',
                    'priority' => false,
                    'customer_segment' => 'regular',
                    'billing_address' => 'Test Sk. No:99, Kadıköy, İstanbul',
                    'billing_email' => 'ali.veli@test.com',
                    'billing_tax_number' => '89012345678',
               ],
          ];

          foreach ($customers as $customerData) {
               $companyIds = $customerData['companies'] ?? [];
               unset($customerData['companies']);

               $customer = Customer::create(array_merge($customerData, [
                    'agency_id' => $agency->id,
                    'tags' => ['gezi', 'kültür'],
                    'interests' => ['tarih', 'doğa'],
               ]));

               if (!empty($companyIds)) {
                    $customer->companies()->sync($companyIds);
               }
          }

          $this->command->info('2 şirket kartı ve ' . count($customers) . ' kişi başarıyla eklendi!');
     }
}
