<?php

namespace App\Http\Controllers;

use Illuminate\View\View;

class AdminController extends Controller
{
    /**
     * Core datasets reused across the admin experience.
     */
    private array $employees = [];
    private array $applications = [];
    private array $attendanceRequests = [];
    private array $jobPostings = [];
    private array $shifts = [];
    private array $payrollSummary = [];
    private array $payrollDetails = [];
    private array $analytics = [];
    private array $companyProfile = [];

    public function __construct()
    {
        $this->employees = [
            [
                'initials' => 'JS',
                'name' => 'John Smith',
                'id' => 'EMP001',
                'email' => 'john.smith@email.com',
                'phone' => '+44 7700 900001',
                'department' => 'Security',
                'designation' => 'Security Guard',
                'status' => 'Active',
                'joined' => '2024-01-15',
            ],
            [
                'initials' => 'SJ',
                'name' => 'Sarah Johnson',
                'id' => 'EMP002',
                'email' => 'sarah.j@email.com',
                'phone' => '+44 7700 900002',
                'department' => 'Warehouse',
                'designation' => 'Warehouse Staff',
                'status' => 'Active',
                'joined' => '2024-02-20',
            ],
            [
                'initials' => 'MC',
                'name' => 'Michael Chen',
                'id' => 'EMP003',
                'email' => 'michael.c@email.com',
                'phone' => '+44 7700 900003',
                'department' => 'Transport',
                'designation' => 'Driver',
                'status' => 'Active',
                'joined' => '2024-03-10',
            ],
            [
                'initials' => 'ED',
                'name' => 'Emma Davis',
                'id' => 'EMP004',
                'email' => 'emma.d@email.com',
                'phone' => '+44 7700 900004',
                'department' => 'Cleaning',
                'designation' => 'Cleaner',
                'status' => 'Active',
                'joined' => '2024-03-15',
            ],
            [
                'initials' => 'JW',
                'name' => 'James Wilson',
                'id' => 'EMP005',
                'email' => 'james.w@email.com',
                'phone' => '+44 7700 900005',
                'department' => 'Security',
                'designation' => 'Security Supervisor',
                'status' => 'Active',
                'joined' => '2023-11-01',
            ],
        ];

        $this->applications = [
            [
                'initials' => 'SJ',
                'name' => 'Sarah Johnson',
                'code' => 'APP001',
                'email' => 'sarah.j@email.com',
                'phone' => '+44 7700 900123',
                'position' => 'Security Guard',
                'experience' => '2 years',
                'location' => 'London',
                'date' => '2025-11-20',
                'status' => 'Applied',
            ],
            [
                'initials' => 'MC',
                'name' => 'Michael Chen',
                'code' => 'APP002',
                'email' => 'michael.c@email.com',
                'phone' => '+44 7700 900124',
                'position' => 'Warehouse Staff',
                'experience' => '1 year',
                'location' => 'Manchester',
                'date' => '2025-11-19',
                'status' => 'Shortlisted',
            ],
            [
                'initials' => 'ED',
                'name' => 'Emma Davis',
                'code' => 'APP003',
                'email' => 'emma.d@email.com',
                'phone' => '+44 7700 900125',
                'position' => 'Driver (HGV)',
                'experience' => '3 years',
                'location' => 'Birmingham',
                'date' => '2025-11-18',
                'status' => 'Interviewed',
            ],
            [
                'initials' => 'JW',
                'name' => 'James Wilson',
                'code' => 'APP004',
                'email' => 'james.w@email.com',
                'phone' => '+44 7700 900126',
                'position' => 'Cleaning Supervisor',
                'experience' => '2 years',
                'location' => 'Leeds',
                'date' => '2025-11-17',
                'status' => 'Hired',
            ],
            [
                'initials' => 'LB',
                'name' => 'Lisa Brown',
                'code' => 'APP005',
                'email' => 'lisa.b@email.com',
                'phone' => '+44 7700 900127',
                'position' => 'Security Guard',
                'experience' => '1 year',
                'location' => 'London',
                'date' => '2025-11-21',
                'status' => 'Applied',
            ],
        ];

        $this->attendanceRequests = [
            [
                'initials' => 'JS',
                'name' => 'John Smith',
                'id' => 'EMP001',
                'date' => '2025-11-21',
                'shift' => 'Morning',
                'location' => 'London Office',
                'check_in' => '09:00 AM',
                'check_out' => '05:00 PM',
                'hours' => '8h',
                'status' => 'Pending',
            ],
            [
                'initials' => 'SJ',
                'name' => 'Sarah Johnson',
                'id' => 'EMP002',
                'date' => '2025-11-21',
                'shift' => 'Evening',
                'location' => 'Manchester Office',
                'check_in' => '02:00 PM',
                'check_out' => '10:00 PM',
                'hours' => '8h',
                'status' => 'Pending',
            ],
        ];

        $this->jobPostings = [
            [
                'title' => 'Security Guard',
                'location' => 'London',
                'type' => 'Full-time',
                'status' => 'Active',
                'department' => 'Security',
                'salary' => '£25,000 - £28,000',
                'experience' => '1-2 years',
                'posted' => '2025-11-15',
                'applicants' => 23,
            ],
            [
                'title' => 'Warehouse Staff',
                'location' => 'Manchester',
                'type' => 'Full-time',
                'status' => 'Active',
                'department' => 'Warehouse',
                'salary' => '£22,000 - £25,000',
                'experience' => '0-1 years',
                'posted' => '2025-11-18',
                'applicants' => 18,
            ],
            [
                'title' => 'Driver (HGV)',
                'location' => 'Birmingham',
                'type' => 'Full-time',
                'status' => 'Active',
                'department' => 'Transport',
                'salary' => '£30,000 - £35,000',
                'experience' => '2-3 years',
                'posted' => '2025-11-20',
                'applicants' => 15,
            ],
            [
                'title' => 'Cleaning Supervisor',
                'location' => 'Leeds',
                'type' => 'Part-time',
                'status' => 'Active',
                'department' => 'Cleaning',
                'salary' => '£20,000 - £23,000',
                'experience' => '1-2 years',
                'posted' => '2025-11-12',
                'applicants' => 8,
            ],
        ];

        $this->shifts = [
            [
                'title' => 'Morning Security Shift',
                'date' => '2025-11-22',
                'status' => 'Open',
                'time' => '6:00 AM - 2:00 PM',
                'location' => 'London Office',
                'department' => 'Security',
                'assignment' => '3 / 5',
                'interested' => 7,
            ],
            [
                'title' => 'Warehouse Evening Shift',
                'date' => '2025-11-22',
                'status' => 'Filled',
                'time' => '2:00 PM - 10:00 PM',
                'location' => 'Manchester Warehouse',
                'department' => 'Warehouse',
                'assignment' => '10 / 10',
                'interested' => 12,
            ],
            [
                'title' => 'Night Cleaning Shift',
                'date' => '2025-11-23',
                'status' => 'Open',
                'time' => '10:00 PM - 6:00 AM',
                'location' => 'Birmingham Complex',
                'department' => 'Cleaning',
                'assignment' => '2 / 4',
                'interested' => 5,
            ],
            [
                'title' => 'Driver Morning Route',
                'date' => '2025-11-22',
                'status' => 'Filled',
                'time' => '7:00 AM - 3:00 PM',
                'location' => 'Leeds Hub',
                'department' => 'Transport',
                'assignment' => '3 / 3',
                'interested' => 4,
            ],
        ];

        $this->payrollSummary = [
            [
                'period' => 'November 2025',
                'employees' => 5,
                'hours' => 800,
                'total' => '£11,600.00',
                'status' => 'Processed',
            ],
            [
                'period' => 'October 2025',
                'employees' => 280,
                'hours' => 44800,
                'total' => '£618,400',
                'status' => 'Completed',
            ],
            [
                'period' => 'September 2025',
                'employees' => 275,
                'hours' => 44000,
                'total' => '£607,000',
                'status' => 'Completed',
            ],
        ];

        $this->payrollDetails = [
            [
                'name' => 'John Smith',
                'id' => 'EMP001',
                'department' => 'Security',
                'hours' => 160,
                'rate' => '£14.50',
                'gross' => '£2320.00',
                'deductions' => '£232.00',
                'net' => '£2088.00',
            ],
            [
                'name' => 'Sarah Johnson',
                'id' => 'EMP002',
                'department' => 'Warehouse',
                'hours' => 160,
                'rate' => '£13.00',
                'gross' => '£2080.00',
                'deductions' => '£208.00',
                'net' => '£1872.00',
            ],
            [
                'name' => 'Michael Chen',
                'id' => 'EMP003',
                'department' => 'Transport',
                'hours' => 160,
                'rate' => '£16.00',
                'gross' => '£2560.00',
                'deductions' => '£256.00',
                'net' => '£2304.00',
            ],
            [
                'name' => 'Emma Davis',
                'id' => 'EMP004',
                'department' => 'Cleaning',
                'hours' => 160,
                'rate' => '£12.50',
                'gross' => '£2000.00',
                'deductions' => '£200.00',
                'net' => '£1800.00',
            ],
            [
                'name' => 'James Wilson',
                'id' => 'EMP005',
                'department' => 'Security',
                'hours' => 160,
                'rate' => '£16.50',
                'gross' => '£2640.00',
                'deductions' => '£264.00',
                'net' => '£2376.00',
            ],
        ];

        $this->analytics = [
            'summary' => [
                [
                    'label' => 'Avg Applications/Month',
                    'value' => '1',
                    'meta' => '+12% from last month',
                    'icon' => 'trend-up',
                    'accent' => '#4d49fc',
                    'accent_soft' => '#eef2ff',
                ],
                [
                    'label' => 'Conversion Rate',
                    'value' => '20.0%',
                    'meta' => '+1.5% improvement',
                    'icon' => 'target',
                    'accent' => '#f97316',
                    'accent_soft' => '#ffedd5',
                ],
                [
                    'label' => 'Avg Attendance',
                    'value' => '93%',
                    'meta' => 'Stable',
                    'icon' => 'calendar-check',
                    'accent' => '#0ea5e9',
                    'accent_soft' => '#e0f2fe',
                ],
                [
                    'label' => 'Active Positions',
                    'value' => '4',
                    'meta' => 'Currently open',
                    'icon' => 'briefcase',
                    'accent' => '#16a34a',
                    'accent_soft' => '#dcfce7',
                ],
            ],
            'applicationTrend' => [
                'labels' => ['June', 'July', 'August', 'September', 'October', 'November'],
                'applications' => [120, 140, 170, 130, 175, 5],
                'hired' => [10, 15, 10, 10, 10, 0],
                'shortlisted' => [40, 45, 50, 40, 50, 0],
            ],
            'attendanceTrend' => [
                'labels' => ['Week 1', 'Week 2', 'Week 3', 'Week 4'],
                'values' => [94, 92, 95, 90],
            ],
            'topPerformers' => [
                ['rank' => '#1', 'name' => 'John Smith', 'department' => 'Security', 'score' => '98%', 'shifts' => '24 shifts'],
                ['rank' => '#2', 'name' => 'Sarah Johnson', 'department' => 'Warehouse', 'score' => '97%', 'shifts' => '23 shifts'],
                ['rank' => '#3', 'name' => 'Michael Chen', 'department' => 'Transport', 'score' => '96%', 'shifts' => '22 shifts'],
                ['rank' => '#4', 'name' => 'Emma Davis', 'department' => 'Cleaning', 'score' => '95%', 'shifts' => '21 shifts'],
            ],
            'distribution' => [
                ['label' => 'Security', 'value' => 2],
                ['label' => 'Warehouse', 'value' => 1],
                ['label' => 'Transport', 'value' => 1],
                ['label' => 'Cleaning', 'value' => 1],
            ],
            'applicationsBreakdown' => [
                ['label' => 'Applied', 'value' => 2],
                ['label' => 'Shortlisted', 'value' => 1],
                ['label' => 'Interviewed', 'value' => 1],
                ['label' => 'Hired', 'value' => 1],
            ],
            'workforce' => [
                ['label' => 'Total Employees', 'value' => 5],
                ['label' => 'Active', 'value' => 5],
                ['label' => 'On Leave', 'value' => 0],
                ['label' => 'Inactive', 'value' => 0],
            ],
            'jobPostings' => [
                ['label' => 'Total Jobs', 'value' => 4],
                ['label' => 'Active', 'value' => 4],
                ['label' => 'Draft', 'value' => 0],
                ['label' => 'Closed', 'value' => 0],
            ],
        ];

        $this->companyProfile = [
            'name' => 'Your Company Ltd',
            'industry' => 'HR Services',
            'email' => 'contact@company.com',
            'phone' => '+44 20 1234 5678',
            'address' => '123 Business Street, London, UK',
        ];
    }

    public function dashboard(): View
    {
        $stats = [
            [
                'label' => 'Total Employees',
                'value' => count($this->employees),
                'meta' => '5 active',
            ],
            [
                'label' => 'Active Jobs',
                'value' => count($this->jobPostings),
                'meta' => '4 open postings',
            ],
            [
                'label' => 'Applications',
                'value' => count($this->applications),
                'meta' => '2 new',
            ],
            [
                'label' => 'Attendance Today',
                'value' => '92%',
                'meta' => '2 pending',
            ],
        ];

        return view('admin.dashboard', [
            'stats' => $stats,
            'quickActions' => ['Add New Employee', 'Post New Job', 'Create Shift', 'Send Announcement'],
            'recentApplications' => array_slice($this->applications, 0, 4),
            'attendanceRequests' => $this->attendanceRequests,
        ]);
    }

    public function employees(): View
    {
        $collection = collect($this->employees);
        $employeeStats = [
            [
                'label' => 'Total Employees',
                'value' => $collection->count(),
                'meta' => 'Company-wide',
                'icon' => 'users',
                'accent' => '#4d49fc',
                'accent_soft' => '#eef2ff',
            ],
            [
                'label' => 'Active',
                'value' => $collection->where('status', 'Active')->count(),
                'meta' => 'Currently on duty',
                'icon' => 'shield',
                'accent' => '#16a34a',
                'accent_soft' => '#dcfce7',
            ],
            [
                'label' => 'On Leave',
                'value' => $collection->where('status', 'On Leave')->count(),
                'meta' => 'Out of office',
                'icon' => 'calendar',
                'accent' => '#f97316',
                'accent_soft' => '#ffedd5',
            ],
            [
                'label' => 'Inactive',
                'value' => $collection->where('status', 'Inactive')->count(),
                'meta' => 'Offboarded',
                'icon' => 'pause',
                'accent' => '#0ea5e9',
                'accent_soft' => '#e0f2fe',
            ],
        ];

        return view('admin.employees', [
            'employeeStats' => $employeeStats,
            'employees' => $this->employees,
            'departments' => $collection->pluck('department')->unique()->values(),
            'statuses' => ['All Status', 'Active', 'On Leave', 'Inactive'],
        ]);
    }

    public function jobManagement(): View
    {
        $jobsCollection = collect($this->jobPostings);
        return view('admin.jobs', [
            'jobStats' => [
                [
                    'label' => 'Active Jobs',
                    'value' => count($this->jobPostings),
                    'meta' => 'Currently open',
                    'icon' => 'briefcase',
                    'accent' => '#4d49fc',
                    'accent_soft' => '#eef2ff',
                ],
                [
                    'label' => 'Total Applications',
                    'value' => count($this->applications),
                    'meta' => 'Across all postings',
                    'icon' => 'inbox',
                    'accent' => '#0ea5e9',
                    'accent_soft' => '#dbeafe',
                ],
                [
                    'label' => 'Shortlisted',
                    'value' => 1,
                    'meta' => 'Ready for interview',
                    'icon' => 'star',
                    'accent' => '#f97316',
                    'accent_soft' => '#ffedd5',
                ],
                [
                    'label' => 'Hired This Month',
                    'value' => 1,
                    'meta' => 'New joiners',
                    'icon' => 'check',
                    'accent' => '#16a34a',
                    'accent_soft' => '#dcfce7',
                ],
            ],
            'jobs' => $this->jobPostings,
            'jobLocations' => $jobsCollection->pluck('location')->unique()->values(),
            'jobStatuses' => ['All Status', 'Active', 'Draft', 'Closed'],
        ]);
    }

    public function applications(): View
    {
        $statusCounts = collect($this->applications)
            ->groupBy('status')
            ->map->count();

        $applicationCards = [
            [
                'label' => 'Total Applications',
                'value' => count($this->applications),
                'meta' => 'Across all postings',
                'icon' => 'inbox',
                'accent' => '#4d49fc',
                'accent_soft' => '#eef2ff',
            ],
            [
                'label' => 'Applied',
                'value' => $statusCounts->get('Applied', 0),
                'meta' => 'Awaiting review',
                'icon' => 'paper-plane',
                'accent' => '#2563eb',
                'accent_soft' => '#dbeafe',
            ],
            [
                'label' => 'Shortlisted',
                'value' => $statusCounts->get('Shortlisted', 0),
                'meta' => 'Ready for interview',
                'icon' => 'star',
                'accent' => '#f97316',
                'accent_soft' => '#ffedd5',
            ],
            [
                'label' => 'Interviewed',
                'value' => $statusCounts->get('Interviewed', 0),
                'meta' => 'Interviews completed',
                'icon' => 'chat',
                'accent' => '#0ea5e9',
                'accent_soft' => '#e0f2fe',
            ],
            [
                'label' => 'Hired',
                'value' => $statusCounts->get('Hired', 0),
                'meta' => 'Offer accepted',
                'icon' => 'check',
                'accent' => '#16a34a',
                'accent_soft' => '#dcfce7',
            ],
        ];

        $applicationFilters = collect([
            'All' => count($this->applications),
            'Applied' => $statusCounts->get('Applied', 0),
            'Shortlisted' => $statusCounts->get('Shortlisted', 0),
            'Interviewed' => $statusCounts->get('Interviewed', 0),
            'Hired' => $statusCounts->get('Hired', 0),
        ])->map(function ($count, $label) {
            return [
                'label' => $label,
                'count' => $count,
                'active' => $label === 'All',
            ];
        })->values();

        return view('admin.applications', [
            'applicationCards' => $applicationCards,
            'applications' => $this->applications,
            'applicationFilters' => $applicationFilters,
        ]);
    }

    public function attendance(): View
    {
        $cards = [
            ['label' => "Today's Attendance", 'value' => '92%', 'meta' => '283 of 308 present'],
            ['label' => 'Pending Approvals', 'value' => count($this->attendanceRequests)],
            ['label' => 'On Leave', 'value' => 8],
            ['label' => 'Absent', 'value' => 276],
        ];

        $tabs = [
            ['label' => 'Pending (2)', 'active' => true],
            ['label' => 'Approved (2)', 'active' => false],
            ['label' => 'Rejected (1)', 'active' => false],
            ['label' => 'All Records (5)', 'active' => false],
        ];

        return view('admin.attendance', [
            'cards' => $cards,
            'tabs' => $tabs,
            'attendance' => $this->attendanceRequests,
        ]);
    }

    public function shifts(): View
    {
        $cards = [
            [
                'label' => 'Total Shifts',
                'value' => 4,
                'meta' => 'All time',
                'icon' => 'calendar',
                'accent' => '#4d49fc',
                'accent_soft' => '#eef2ff',
            ],
            [
                'label' => 'Open Shifts',
                'value' => 2,
                'meta' => 'Need assignment',
                'icon' => 'sun',
                'accent' => '#f97316',
                'accent_soft' => '#ffedd5',
            ],
            [
                'label' => 'Filled Shifts',
                'value' => 2,
                'meta' => 'Fully staffed',
                'icon' => 'check-circle',
                'accent' => '#16a34a',
                'accent_soft' => '#dcfce7',
            ],
            [
                'label' => 'Total Hours',
                'value' => 176,
                'meta' => 'Scheduled',
                'icon' => 'clock',
                'accent' => '#0ea5e9',
                'accent_soft' => '#e0f2fe',
            ],
        ];

        return view('admin.shifts', [
            'cards' => $cards,
            'shifts' => array_map(function ($shift) {
                $chip = match ($shift['status']) {
                    'Open' => ['label' => 'Open', 'tone' => 'open'],
                    'Filled' => ['label' => 'Filled', 'tone' => 'filled'],
                    default => ['label' => $shift['status'], 'tone' => 'default'],
                };

                return $shift + ['chip' => $chip];
            }, $this->shifts),
        ]);
    }

    public function payroll(): View
    {
        $cards = [
            ['label' => 'This Month Payroll', 'value' => '£11,600.00', 'meta' => '5 employees'],
            ['label' => 'Total Hours', 'value' => '800', 'meta' => 'This month'],
            ['label' => 'Avg Hourly Rate', 'value' => '£14.50', 'meta' => 'Across all roles'],
            ['label' => 'Pending Approvals', 'value' => '12', 'meta' => 'Weekly hours'],
        ];

        return view('admin.payroll', [
            'cards' => $cards,
            'summary' => $this->payrollSummary,
            'details' => $this->payrollDetails,
        ]);
    }

    public function analytics(): View
    {
        return view('admin.analytics', [
            'summary' => $this->analytics['summary'],
            'applicationTrend' => $this->analytics['applicationTrend'],
            'attendanceTrend' => $this->analytics['attendanceTrend'],
            'topPerformers' => $this->analytics['topPerformers'],
            'distribution' => $this->analytics['distribution'],
            'applicationsBreakdown' => $this->analytics['applicationsBreakdown'],
            'workforce' => $this->analytics['workforce'],
            'jobPostings' => $this->analytics['jobPostings'],
        ]);
    }

    public function settings(): View
    {
        return view('admin.settings', [
            'profile' => $this->companyProfile,
        ]);
    }
}

