<?php

namespace App\Modules\Dashboard\Repositories;

use App\Models\User;
use App\Modules\Admin\Tenancy\Models\Tenant;
use App\Modules\HR\Attendance\Repositories\AttendanceDashboardRepository;
use App\Modules\HR\Departments\Models\Department;
use App\Modules\HR\Shifts\Repositories\ShiftStatisticsRepository;
use App\Modules\HR\Employees\Models\Employee;
use App\Modules\HR\Positions\Models\Position;

class DashboardStatsRepository
{
    public function __construct(
        private readonly AttendanceDashboardRepository $attendanceDashboard,
        private readonly ShiftStatisticsRepository $shiftStats,
    ) {}

    /**
     * @param  list<string>  $enabledSlugs
     * @return list<array{key: string, value: int, icon: string}>
     */
    public function statMetricsFor(Tenant $tenant, array $enabledSlugs, ?User $user = null): array
    {
        $metrics = [];
        $slugSet = array_flip($enabledSlugs);

        if (isset($slugSet['hr'])) {
            $metrics[] = [
                'key' => 'total_departments',
                'value' => Department::query()->count(),
                'icon' => 'fe fe-briefcase',
            ];
            $metrics[] = [
                'key' => 'total_positions',
                'value' => Position::query()->count(),
                'icon' => 'fe fe-layers',
            ];
            $metrics[] = [
                'key' => 'total_employees',
                'value' => Employee::query()->count(),
                'icon' => 'fe fe-users',
            ];
            $metrics[] = [
                'key' => 'active_employees',
                'value' => Employee::query()->where('is_active', true)->count(),
                'icon' => 'fe fe-user-check',
            ];

            if ($user !== null) {
                $tid = (int) $tenant->id;
                if ($user->can('attendance.view')) {
                    $metrics[] = [
                        'key' => 'attendance_present_today',
                        'value' => $this->attendanceDashboard->presentTodayCount($tid),
                        'icon' => 'fe fe-user-check',
                    ];
                    $metrics[] = [
                        'key' => 'attendance_absent_today',
                        'value' => $this->attendanceDashboard->absentTodayCount($tid),
                        'icon' => 'fe fe-user-x',
                    ];
                    $metrics[] = [
                        'key' => 'attendance_late_today',
                        'value' => $this->attendanceDashboard->lateTodayCount($tid),
                        'icon' => 'fe fe-clock',
                    ];
                }
                if ($user->can('leave.view')) {
                    $metrics[] = [
                        'key' => 'leave_pending_count',
                        'value' => $this->attendanceDashboard->pendingLeaveRequestsCount($tid),
                        'icon' => 'fe fe-calendar',
                    ];
                }
                if ($user->can('shifts.view')) {
                    $metrics[] = [
                        'key' => 'shifts_active_count',
                        'value' => $this->shiftStats->activeShiftsCount($tid),
                        'icon' => 'fe fe-clock',
                    ];
                    $metrics[] = [
                        'key' => 'employees_without_shift_count',
                        'value' => $this->shiftStats->employeesWithoutActiveShiftCount($tid),
                        'icon' => 'fe fe-user-x',
                    ];
                }
            }
        }

        if (isset($slugSet['inventory'])) {
            $metrics[] = [
                'key' => 'total_products',
                'value' => 0,
                'icon' => 'fe fe-package',
            ];
        }

        if (isset($slugSet['finance'])) {
            $metrics[] = [
                'key' => 'total_accounts',
                'value' => 0,
                'icon' => 'fe fe-dollar-sign',
            ];
        }

        if (isset($slugSet['crm'])) {
            $metrics[] = [
                'key' => 'total_contacts',
                'value' => 0,
                'icon' => 'fe fe-user-plus',
            ];
        }

        if (isset($slugSet['sales'])) {
            $metrics[] = [
                'key' => 'total_orders',
                'value' => 0,
                'icon' => 'fe fe-shopping-cart',
            ];
        }

        return $metrics;
    }
}
