<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $users = [
            [
                'first_name' => 'Super',
                'last_name' => 'Admin',
                'email' => 'a@a.com',
                'password' => Hash::make('1'),
                'phone' => '+905551234567',
                'national_id' => '12345678901',
                'city' => 'Istanbul',
                'country' => 'TR',
                'language' => 'tr',
                'is_active' => true,
                'is_verified' => true,
                'email_verified_at' => now(),
                'role' => 'super-admin',
                'agency_id' => null,
                'is_agency_owner' => false,
            ],
            [
                'first_name' => 'System',
                'last_name' => 'Admin',
                'email' => 'systemadmin@example.com',
                'password' => Hash::make('password123'),
                'phone' => '+905551234568',
                'national_id' => '12345678902',
                'city' => 'Ankara',
                'country' => 'TR',
                'language' => 'tr',
                'is_active' => true,
                'is_verified' => true,
                'email_verified_at' => now(),
                'role' => 'system-admin',
                'agency_id' => null,
                'is_agency_owner' => false,
            ],
            [
                'first_name' => 'Mehmet',
                'last_name' => 'Gürüz',
                'email' => 'd@d.com',
                'password' => Hash::make('1'),
                'phone' => '+905551234569',
                'national_id' => '12345678903',
                'city' => 'Istanbul',
                'country' => 'TR',
                'language' => 'tr',
                'position' => 'CEO',
                'hired_at' => '2020-01-15',
                'is_active' => true,
                'is_verified' => true,
                'email_verified_at' => now(),
                'role' => 'agency-owner',
                'agency_id' => 1,
                'is_agency_owner' => true,
            ],
            [
                'first_name' => 'Ayşe',
                'last_name' => 'Demir',
                'email' => 'ayse@turkishtravel.com',
                'password' => Hash::make('password123'),
                'phone' => '+905551234570',
                'national_id' => '12345678904',
                'city' => 'Istanbul',
                'country' => 'TR',
                'language' => 'tr',
                'position' => 'Operations Manager',
                'hired_at' => '2021-03-10',
                'is_active' => true,
                'is_verified' => true,
                'email_verified_at' => now(),
                'role' => 'agency-manager',
                'agency_id' => 1,
                'is_agency_owner' => false,
            ],
            [
                'first_name' => 'Ali',
                'last_name' => 'Yılmaz',
                'email' => 'ali@turkishtravel.com',
                'password' => Hash::make('password123'),
                'phone' => '+905551234571',
                'national_id' => '12345678905',
                'city' => 'Istanbul',
                'country' => 'TR',
                'language' => 'tr',
                'position' => 'Tour Coordinator',
                'hired_at' => '2022-05-20',
                'is_active' => true,
                'is_verified' => true,
                'email_verified_at' => now(),
                'role' => 'agency-staff',
                'agency_id' => 1,
                'is_agency_owner' => false,
            ],
            [
                'first_name' => 'Fatma',
                'last_name' => 'Kaya',
                'email' => 'fatma@turkishtravel.com',
                'password' => Hash::make('password123'),
                'phone' => '+905551234572',
                'national_id' => '12345678906',
                'city' => 'Istanbul',
                'country' => 'TR',
                'language' => 'tr',
                'position' => 'Sales Representative',
                'hired_at' => '2023-02-15',
                'is_active' => true,
                'is_verified' => true,
                'email_verified_at' => now(),
                'role' => 'sales-agent',
                'agency_id' => 1,
                'is_agency_owner' => false,
            ],
            [
                'first_name' => 'Can',
                'last_name' => 'Türk',
                'email' => 'can@anatoliatravel.com',
                'password' => Hash::make('password123'),
                'phone' => '+905551234573',
                'national_id' => '12345678907',
                'city' => 'Antalya',
                'country' => 'TR',
                'language' => 'tr',
                'position' => 'Founder',
                'hired_at' => '2019-06-01',
                'is_active' => true,
                'is_verified' => true,
                'email_verified_at' => now(),
                'role' => 'agency-owner',
                'agency_id' => 2,
                'is_agency_owner' => true,
            ],
            [
                'first_name' => 'Zeynep',
                'last_name' => 'Aksoy',
                'email' => 'zeynep@anatoliatravel.com',
                'password' => Hash::make('password123'),
                'phone' => '+905551234574',
                'national_id' => '12345678908',
                'city' => 'Antalya',
                'country' => 'TR',
                'language' => 'tr',
                'position' => 'Sales Manager',
                'hired_at' => '2021-08-10',
                'is_active' => true,
                'is_verified' => true,
                'email_verified_at' => now(),
                'role' => 'agency-manager',
                'agency_id' => 2,
                'is_agency_owner' => false,
            ],
            [
                'first_name' => 'Emre',
                'last_name' => 'Çelik',
                'email' => 'emre@anatoliatravel.com',
                'password' => Hash::make('password123'),
                'phone' => '+905551234575',
                'national_id' => '12345678909',
                'city' => 'Antalya',
                'country' => 'TR',
                'language' => 'tr',
                'position' => 'Customer Service',
                'hired_at' => '2023-01-15',
                'is_active' => true,
                'is_verified' => true,
                'email_verified_at' => now(),
                'role' => 'agency-staff',
                'agency_id' => 2,
                'is_agency_owner' => false,
            ],
            [
                'first_name' => 'Deniz',
                'last_name' => 'Arslan',
                'email' => 'deniz@anatoliatravel.com',
                'password' => Hash::make('password123'),
                'phone' => '+905551234576',
                'national_id' => '12345678910',
                'city' => 'Antalya',
                'country' => 'TR',
                'language' => 'tr',
                'position' => 'Sales Agent',
                'hired_at' => '2023-04-01',
                'is_active' => true,
                'is_verified' => true,
                'email_verified_at' => now(),
                'role' => 'sales-agent',
                'agency_id' => 2,
                'is_agency_owner' => false,
            ],
        ];

        foreach ($users as $userData) {
            $roleName = $userData['role'];
            $agencyId = $userData['agency_id'] ?? null;
            $isOwner = $userData['is_agency_owner'] ?? false;
            $position = $userData['position'] ?? null;
            $hiredAt = $userData['hired_at'] ?? null;

            unset($userData['role'], $userData['agency_id'], $userData['is_agency_owner'], $userData['position'], $userData['hired_at']);

            $user = User::create($userData);

            if ($agencyId) {
                // Create relationship record
                $staff = \App\Models\Agency\AgencyStaff::create([
                    'agency_id' => $agencyId,
                    'user_id' => $user->id,
                    'first_name' => $user->first_name,
                    'last_name' => $user->last_name,
                    'email' => $user->email,
                    'phone' => $user->phone,
                    'position' => $position,
                    'hired_at' => $hiredAt,
                    'is_active' => true,
                    'is_agency_owner' => $isOwner,
                    'can_login' => true,
                ]);

                // Assign role within the agency (Team)
                setPermissionsTeamId($agencyId);
                $user->assignRole($roleName);

                // d@d.com için 2. acenteyi ekle
                if ($user->email === 'd@d.com' && $agencyId === 1) {
                    $extraAgencyId = 2;
                    \App\Models\Agency\AgencyStaff::create([
                        'agency_id' => $extraAgencyId,
                        'user_id' => $user->id,
                        'first_name' => $user->first_name,
                        'last_name' => $user->last_name,
                        'email' => $user->email,
                        'phone' => $user->phone,
                        'position' => 'Partner',
                        'hired_at' => $hiredAt,
                        'is_active' => true,
                        'is_agency_owner' => false,
                        'can_login' => true,
                    ]);

                    setPermissionsTeamId($extraAgencyId);
                    $user->assignRole('agency-owner');
                }
            } else {
                // Global roles (like super-admin) don't need a team id
                setPermissionsTeamId(null);
                $user->assignRole($roleName);
            }
        }
    }
}
