<?php

use Illuminate\Database\Seeder;
use App\Modules\Bookings\Task;
use App\Modules\Bookings\Comment;
use App\Modules\Clients\Client;
use App\Modules\Clients\Customer;
use App\Models\Status;
use App\Models\Severity;
use App\Models\User;

class TasksBookingsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Create statuses for different task types
        $statuses = [
            // Repair statuses
            ['name' => 'Booked In', 'for' => 'repairs', 'completed' => 0],
            ['name' => 'In Progress', 'for' => 'repairs', 'completed' => 0],
            ['name' => 'Awaiting Parts', 'for' => 'repairs', 'completed' => 0],
            ['name' => 'Ready for QA', 'for' => 'repairs', 'completed' => 0],
            ['name' => 'QA Complete', 'for' => 'repairs', 'completed' => 0],
            ['name' => 'Ready for Collection', 'for' => 'repairs', 'completed' => 1],
            ['name' => 'Completed', 'for' => 'repairs', 'completed' => 1],
            
            // Support statuses
            ['name' => 'New', 'for' => 'repairs', 'completed' => 0],
            ['name' => 'In Progress', 'for' => 'repairs', 'completed' => 0],
            ['name' => 'Waiting for Customer', 'for' => 'repairs', 'completed' => 0],
            ['name' => 'Resolved', 'for' => 'repairs', 'completed' => 1],
            
            // Onsite statuses
            ['name' => 'Scheduled', 'for' => 'onsite', 'completed' => 0],
            ['name' => 'En Route', 'for' => 'onsite', 'completed' => 0],
            ['name' => 'On Site', 'for' => 'onsite', 'completed' => 0],
            ['name' => 'Complete', 'for' => 'onsite', 'completed' => 1],
        ];

        foreach ($statuses as $status) {
            Status::firstOrCreate(
                ['name' => $status['name'], 'for' => $status['for']],
                $status
            );
        }

        // Create severity levels
        $severities = [
            ['name' => 'Low', 'priority' => 1],
            ['name' => 'Medium', 'priority' => 2],
            ['name' => 'High', 'priority' => 3],
            ['name' => 'Critical', 'priority' => 4],
        ];

        foreach ($severities as $severity) {
            Severity::firstOrCreate(['name' => $severity['name']], $severity);
        }

        // Get reference data
        $clients = Client::all();
        $customers = Customer::all();
        $users = User::where('active', 1)->get();
        $repairStatuses = Status::where('for', 'repairs')->get();
        $onsiteStatuses = Status::where('for', 'onsite')->get();
        $severities = Severity::all();

        // Create repair tasks
        $repairTasks = [
            [
                'title' => 'Laptop Screen Replacement',
                'problem' => 'Customer reports cracked laptop screen. Device still boots but display is severely damaged with multiple cracks across the panel.',
                'type' => 'repair',
                'labour' => 45.00,
            ],
            [
                'title' => 'Desktop PC Not Booting',
                'problem' => 'Desktop computer will not power on. Customer reports no lights or fan activity when power button is pressed.',
                'type' => 'repair',
                'labour' => 65.00,
            ],
            [
                'title' => 'Virus Removal and Cleanup',
                'problem' => 'Multiple popup advertisements, slow performance, and suspicious network activity detected. Full system cleanup required.',
                'type' => 'repair',
                'labour' => 75.00,
            ],
            [
                'title' => 'Hard Drive Data Recovery',
                'problem' => 'Hard drive making clicking sounds and not recognized by system. Customer has important family photos that need recovering.',
                'type' => 'repair',
                'labour' => 120.00,
            ],
            [
                'title' => 'MacBook Water Damage',
                'problem' => 'MacBook Pro suffered liquid spill. Device powers on intermittently but keyboard and trackpad are unresponsive.',
                'type' => 'repair',
                'labour' => 95.00,
            ]
        ];

        foreach ($repairTasks as $taskData) {
            $isCustomer = rand(0, 1);
            $client = $isCustomer ? $customers->random() : $clients->random();
            
            $task = Task::create(array_merge($taskData, [
                'client_id' => $client->id,
                'client_type' => $isCustomer ? 'customer' : 'client',
                'assigned_to' => $users->random()->id,
                'status_id' => $repairStatuses->random()->id,
                'severity_id' => $severities->random()->id,
                'time_spent' => rand(30, 240),
                'pin' => rand(1000, 9999),
                'created_by' => $users->random()->id,
                'updated_by' => $users->random()->id,
                'created_at' => now()->subDays(rand(1, 30)),
                'updated_at' => now()->subDays(rand(0, 7))
            ]));

            // Add some comments to tasks
            if (rand(0, 1)) {
                Comment::create([
                    'related_id' => $task->id,
                    'related_type' => 'task',
                    'message' => 'Initial diagnosis completed. ' . ['Requires new parts to be ordered.', 'Hardware issue confirmed.', 'Software corruption detected.', 'Customer contacted for approval.'][rand(0, 3)],
                    'public' => rand(0, 1),
                    'created_by' => $users->random()->id,
                    'created_at' => $task->created_at->addHours(rand(1, 24))
                ]);
            }
        }

        // Create support tasks
        $supportTasks = [
            [
                'title' => 'Email Setup Assistance',
                'problem' => 'Customer needs help setting up email on new iPhone. Outlook configuration required for business account.',
                'type' => 'support',
                'labour' => 25.00,
            ],
            [
                'title' => 'Network Connectivity Issues',
                'problem' => 'Office computers losing network connection intermittently. WiFi appears stable but wired connections dropping.',
                'type' => 'support',
                'labour' => 55.00,
            ],
            [
                'title' => 'Software Installation',
                'problem' => 'Customer requires Adobe Creative Suite installation and license activation on 3 workstations.',
                'type' => 'support',
                'labour' => 35.00,
            ],
            [
                'title' => 'Printer Setup and Configuration',
                'problem' => 'New HP LaserJet printer needs to be configured for network printing across 5 computers in small office.',
                'type' => 'support',
                'labour' => 40.00,
            ]
        ];

        foreach ($supportTasks as $taskData) {
            $isCustomer = rand(0, 1);
            $client = $isCustomer ? $customers->random() : $clients->random();
            
            Task::create(array_merge($taskData, [
                'client_id' => $client->id,
                'client_type' => $isCustomer ? 'customer' : 'client',
                'assigned_to' => $users->random()->id,
                'status_id' => $repairStatuses->random()->id,
                'severity_id' => $severities->random()->id,
                'time_spent' => rand(15, 120),
                'created_by' => $users->random()->id,
                'updated_by' => $users->random()->id,
                'created_at' => now()->subDays(rand(1, 14)),
                'updated_at' => now()->subDays(rand(0, 3))
            ]));
        }

        // Create onsite tasks
        $onsiteTasks = [
            [
                'title' => 'Server Maintenance',
                'problem' => 'Quarterly server maintenance required. Updates, backup verification, and performance optimization needed.',
                'type' => 'onsite',
                'labour' => 180.00,
                'prebook' => 1,
            ],
            [
                'title' => 'Network Infrastructure Upgrade',
                'problem' => 'Upgrade office network switches and configure new VLAN structure for improved security and performance.',
                'type' => 'onsite',
                'labour' => 240.00,
                'prebook' => 1,
            ],
            [
                'title' => 'Workstation Deployment',
                'problem' => 'Deploy and configure 8 new workstations with domain join, software installation, and user profile setup.',
                'type' => 'onsite',
                'labour' => 320.00,
                'prebook' => 1,
            ]
        ];

        foreach ($onsiteTasks as $taskData) {
            $client = $clients->random();
            $prebookDate = $taskData['prebook'] ? now()->addDays(rand(1, 14)) : null;
            
            Task::create(array_merge($taskData, [
                'client_id' => $client->id,
                'client_type' => 'client',
                'site_id' => $client->sites()->first()?->id,
                'assigned_to' => $users->random()->id,
                'status_id' => $onsiteStatuses->random()->id,
                'severity_id' => $severities->random()->id,
                'time_spent' => rand(60, 480),
                'prebook_date' => $prebookDate,
                'created_by' => $users->random()->id,
                'updated_by' => $users->random()->id,
                'created_at' => now()->subDays(rand(1, 21)),
                'updated_at' => now()->subDays(rand(0, 5))
            ]));
        }

        // Create some completed tasks with invoicing
        $completedTasks = Task::where('type', 'repair')->take(3)->get();
        foreach ($completedTasks as $task) {
            $completedStatus = Status::where('for', 'repairs')->where('completed', 1)->first();
            $task->update([
                'status_id' => $completedStatus->id,
                'completed' => 1,
                'completed_at' => now()->subDays(rand(1, 7)),
                'invoiced' => rand(0, 1),
                'paid' => rand(0, 1)
            ]);
        }

        $this->command->info('Created ' . Task::count() . ' tasks with various types and statuses');
        $this->command->info('Created ' . Comment::count() . ' task comments');
    }
} 