<?php

use Illuminate\Database\Seeder;
use App\Modules\Clients\Client;
use App\Modules\Clients\Customer;
use App\Modules\Clients\Site;
use App\Modules\Clients\SiteUser;
use App\Models\Contract;
use App\Models\Sla;

class ClientsCustomersSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Create contracts first
        $contracts = [
            'Standard Support Contract',
            'Premium Support Contract', 
            'Enterprise Support Contract',
            'Basic Maintenance Contract',
            'Comprehensive Care Contract'
        ];

        foreach ($contracts as $contractName) {
            Contract::firstOrCreate(['name' => $contractName]);
        }

        // Create SLA levels
        $slaLevels = [
            [
                'name' => 'Standard',
                'time_initial' => 240, // 4 hours
                'time_fix' => 1440, // 24 hours
                'badge' => 'default'
            ],
            [
                'name' => 'Priority',
                'time_initial' => 120, // 2 hours
                'time_fix' => 480, // 8 hours
                'badge' => 'warning'
            ],
            [
                'name' => 'Critical',
                'time_initial' => 60, // 1 hour
                'time_fix' => 240, // 4 hours
                'badge' => 'important'
            ]
        ];

        foreach ($slaLevels as $sla) {
            Sla::firstOrCreate(['name' => $sla['name']], $sla);
        }

        // Create business clients
        $businessClients = [
            [
                'name' => 'TechCorp Solutions Ltd',
                'website' => 'https://techcorp.co.uk',
                'contract_price' => 2500.00,
                'contract_monthly_allowance' => 40,
                'enable_feedback' => 1,
                'sites' => [
                    [
                        'name' => 'Head Office',
                        'address' => '123 Business Park',
                        'address2' => 'Technology Quarter',
                        'postcode' => 'M1 4AB',
                        'primary_phone' => '01612345001',
                        'users' => [
                            ['first_name' => 'James', 'last_name' => 'Wilson', 'email' => 'james.wilson@techcorp.co.uk', 'position' => 'IT Manager', 'authority' => 1],
                            ['first_name' => 'Alice', 'last_name' => 'Brown', 'email' => 'alice.brown@techcorp.co.uk', 'position' => 'Office Manager', 'authority' => 0]
                        ]
                    ],
                    [
                        'name' => 'Warehouse',
                        'address' => '456 Industrial Estate',
                        'postcode' => 'M2 5CD',
                        'primary_phone' => '01612345002',
                        'users' => [
                            ['first_name' => 'Bob', 'last_name' => 'Smith', 'email' => 'bob.smith@techcorp.co.uk', 'position' => 'Warehouse Manager', 'authority' => 1]
                        ]
                    ]
                ]
            ],
            [
                'name' => 'Digital Marketing Agency',
                'website' => 'https://digitalagency.com',
                'contract_price' => 1800.00,
                'contract_monthly_allowance' => 30,
                'enable_feedback' => 1,
                'sites' => [
                    [
                        'name' => 'Creative Studio',
                        'address' => '789 Creative Quarter',
                        'postcode' => 'M3 6EF',
                        'primary_phone' => '01612345003',
                        'users' => [
                            ['first_name' => 'Sophie', 'last_name' => 'Taylor', 'email' => 'sophie@digitalagency.com', 'position' => 'Creative Director', 'authority' => 1],
                            ['first_name' => 'Mark', 'last_name' => 'Johnson', 'email' => 'mark@digitalagency.com', 'position' => 'Account Manager', 'authority' => 0]
                        ]
                    ]
                ]
            ],
            [
                'name' => 'Healthcare Practice',
                'website' => 'https://healthcarepractice.nhs.uk',
                'contract_price' => 3200.00,
                'contract_monthly_allowance' => 50,
                'enable_feedback' => 1,
                'sites' => [
                    [
                        'name' => 'Main Surgery',
                        'address' => '321 Health Street',
                        'postcode' => 'M4 7GH',
                        'primary_phone' => '01612345004',
                        'users' => [
                            ['first_name' => 'Dr. Sarah', 'last_name' => 'Williams', 'email' => 'sarah.williams@practice.nhs.uk', 'position' => 'Practice Manager', 'authority' => 1]
                        ]
                    ]
                ]
            ],
            [
                'name' => 'Admin Demo Ltd',
                'website' => 'https://admindemo.example',
                'contract_price' => 999.99,
                'contract_monthly_allowance' => 10,
                'enable_feedback' => 1,
                'sites' => [
                    [
                        'name' => 'Admin HQ',
                        'address' => '1 Admin Street',
                        'postcode' => 'AD1 2MN',
                        'primary_phone' => '01610000001',
                        'users' => [
                            ['first_name' => 'Admin', 'last_name' => 'User', 'email' => 'admin@admindemo.example', 'position' => 'Administrator', 'authority' => 1],
                            ['first_name' => 'Test', 'last_name' => 'Person', 'email' => 'test@admindemo.example', 'position' => 'Tester', 'authority' => 0]
                        ]
                    ]
                ]
            ],
            [
                'name' => 'Fake Enterprises',
                'website' => 'https://fakeenterprises.biz',
                'contract_price' => 1500.00,
                'contract_monthly_allowance' => 20,
                'enable_feedback' => 1,
                'sites' => [
                    [
                        'name' => 'Fake HQ',
                        'address' => '404 Not Found Ave',
                        'postcode' => 'FA4 KE1',
                        'primary_phone' => '01619999999',
                        'users' => [
                            ['first_name' => 'Fay', 'last_name' => 'Kerr', 'email' => 'fay.kerr@fakeenterprises.biz', 'position' => 'CEO', 'authority' => 1]
                        ]
                    ]
                ]
            ],
            [
                'name' => 'Sample Solutions',
                'website' => 'https://samplesolutions.test',
                'contract_price' => 2000.00,
                'contract_monthly_allowance' => 25,
                'enable_feedback' => 1,
                'sites' => [
                    [
                        'name' => 'Sample Office',
                        'address' => '12 Example Road',
                        'postcode' => 'SA1 MPL',
                        'primary_phone' => '01618888888',
                        'users' => [
                            ['first_name' => 'Sam', 'last_name' => 'Ple', 'email' => 'sam.ple@samplesolutions.test', 'position' => 'Manager', 'authority' => 1]
                        ]
                    ]
                ]
            ]
        ];

        $contracts = Contract::all();
        $slas = Sla::all();

        foreach ($businessClients as $clientData) {
            $sites = $clientData['sites'];
            unset($clientData['sites']);

            $client = Client::firstOrCreate(
                ['name' => $clientData['name']],
                array_merge($clientData, [
                    'contract_id' => $contracts->random()->id,
                    'sla_id' => $slas->random()->id,
                    'contract_start_date' => now()->subMonths(rand(1, 24)),
                    'contract_end_date' => now()->addMonths(rand(6, 36)),
                    'created_at' => now()->subDays(rand(30, 365))
                ])
            );

            // Create sites for this client
            foreach ($sites as $siteData) {
                $users = $siteData['users'];
                unset($siteData['users']);

                $site = Site::firstOrCreate(
                    ['name' => $siteData['name'], 'client_id' => $client->id],
                    array_merge($siteData, ['client_id' => $client->id])
                );

                // Set primary site if not set
                if (!$client->primary_site) {
                    $client->update(['primary_site' => $site->id]);
                }

                // Create site users
                foreach ($users as $userData) {
                    $siteUser = SiteUser::firstOrCreate(
                        ['email' => $userData['email']],
                        array_merge($userData, [
                            'site_id' => $site->id,
                            'phone' => '0161234' . rand(1000, 9999),
                            'mobile' => '0770090' . rand(1000, 9999)
                        ])
                    );

                    // Set primary contact if not set and user has authority
                    if (!$client->primary_contact && $userData['authority']) {
                        $client->update(['primary_contact' => $siteUser->id]);
                    }
                }
            }
        }

        // Create individual customers
        $customers = [
            [
                'first_name' => 'John',
                'last_name' => 'Doe',
                'email' => 'john.doe@email.com',
                'phone' => '01612345010',
                'mobile' => '07700900010',
                'address' => '10 Residential Street',
                'town' => 'Manchester',
                'county' => 'Greater Manchester',
                'postcode' => 'M10 1AA'
            ],
            [
                'first_name' => 'Jane',
                'last_name' => 'Smith',
                'email' => 'jane.smith@email.com',
                'phone' => '01612345011',
                'mobile' => '07700900011',
                'address' => '25 Oak Avenue',
                'town' => 'Stockport',
                'county' => 'Greater Manchester',
                'postcode' => 'SK1 2BB'
            ],
            [
                'first_name' => 'Michael',
                'last_name' => 'Johnson',
                'email' => 'michael.johnson@email.com',
                'phone' => '01612345012',
                'mobile' => '07700900012',
                'address' => '42 Elm Close',
                'town' => 'Oldham',
                'county' => 'Greater Manchester',
                'postcode' => 'OL1 3CC'
            ],
            [
                'first_name' => 'Emily',
                'last_name' => 'Davis',
                'email' => 'emily.davis@email.com',
                'phone' => '01612345013',
                'mobile' => '07700900013',
                'address' => '78 Maple Road',
                'town' => 'Salford',
                'county' => 'Greater Manchester',
                'postcode' => 'M5 4DD'
            ],
            [
                'first_name' => 'Robert',
                'last_name' => 'Wilson',
                'email' => 'robert.wilson@email.com',
                'phone' => '01612345014',
                'mobile' => '07700900014',
                'address' => '156 Pine Street',
                'town' => 'Bolton',
                'county' => 'Greater Manchester',
                'postcode' => 'BL1 5EE'
            ]
        ];

        foreach ($customers as $customerData) {
            Customer::firstOrCreate(
                ['email' => $customerData['email']],
                array_merge($customerData, [
                    'phone2' => '0161234' . rand(5000, 9999),
                    'address2' => rand(0, 1) ? 'Apartment ' . rand(1, 20) : null,
                    'enable_feedback' => 1,
                    'created_at' => now()->subDays(rand(7, 180))
                ])
            );
        }

        $this->command->info('Created ' . Client::count() . ' clients with sites and users');
        $this->command->info('Created ' . Customer::count() . ' individual customers');
    }
} 