<?php

namespace Database\Seeders;

use App\Models\Customer\Customer;
use App\Models\Customer\CustomerActivity;
use App\Models\Agency\Agency;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

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

          $staff = \App\Models\Agency\AgencyStaff::where('agency_id', $agency->id)->whereNotNull('user_id')->first();
          $user = $staff ? $staff->user : null;
          $customers = Customer::where('agency_id', $agency->id)->get();

          if ($customers->isEmpty())
               return;

          foreach ($customers as $customer) {
               // Her müşteri için 3-5 arası rastgele aktivite
               $activityCount = rand(3, 5);

               for ($i = 0; $i < $activityCount; $i++) {
                    $type = $this->getRandomActivityType();
                    $date = Carbon::now()->subDays(rand(1, 100));

                    CustomerActivity::create([
                         'agency_id' => $agency->id,
                         'customer_id' => $customer->id,
                         'activity_type' => $type,
                         'subject' => $this->getActivitySubject($type),
                         'description' => 'Müşteri ile yapılan görüşme veya işlem detayı buraya gelir.',
                         'direction' => rand(0, 1) ? 'inbound' : 'outbound',
                         'contact_method' => $this->getContactMethod($type),
                         'activity_date' => $date,
                         'status' => 'completed',
                         'priority' => 'normal',
                         'related_type' => Customer::class,
                         'related_id' => $customer->id,
                         'performed_by' => $user->id ?? 1,
                         'created_by' => $user->id ?? 1,
                    ]);
               }
          }

          $this->command->info('Müşteri aktiviteleri başarıyla eklendi!');
     }

     private function getRandomActivityType()
     {
          $types = ['call', 'email', 'meeting', 'note', 'sms', 'whatsapp'];
          return $types[array_rand($types)];
     }

     private function getActivitySubject($type)
     {
          $subjects = [
               'call' => 'Tur Bilgilendirme Araması',
               'email' => 'Teklif Gönderimi',
               'meeting' => 'Ofis Ziyareti',
               'note' => 'Genel Müşteri Notu',
               'sms' => 'Rezervasyon Onay Mesajı',
               'whatsapp' => 'İndirim Kuponu Gönderimi',
          ];
          return $subjects[$type] ?? 'Müşteri İletişimi';
     }

     private function getContactMethod($type)
     {
          $methods = [
               'call' => 'phone',
               'email' => 'email',
               'meeting' => 'in_person',
               'note' => null,
               'sms' => 'sms',
               'whatsapp' => 'whatsapp',
          ];
          return $methods[$type] ?? null;
     }
}
