<?php

namespace App\Modules\Dashboard\Services;

use App\Models\User;
use App\Modules\Dashboard\DTOs\OperationalAlertItem;
use App\Modules\Dashboard\DTOs\OperationalSnapshot;
use App\Modules\Dashboard\Support\AlertSeverity;
use Illuminate\Support\Facades\Route;

class OperationalAttentionService
{
    /**
     * @param  list<string>  $enabledSlugs
     * @return list<OperationalAlertItem>
     */
    public function prioritizedAlerts(
        User $user,
        ?OperationalSnapshot $snapshot,
        array $enabledSlugs,
        int $billingIssues,
        int $planRisks,
    ): array {
        if ($user->tenant === null || $user->isAdmin()) {
            return [];
        }
        $hr = in_array('hr', $enabledSlugs, true);
        $alerts = [];
        if ($snapshot !== null && $hr) {
            $alerts = array_merge($alerts, $this->hrAlerts($user, $snapshot));
        }
        $alerts = array_merge($alerts, $this->billingAlerts($billingIssues, $planRisks));

        usort($alerts, fn (OperationalAlertItem $a, OperationalAlertItem $b): int => $b->priority <=> $a->priority);

        return array_slice($alerts, 0, 8);
    }

    /**
     * @return list<OperationalAlertItem>
     */
    private function hrAlerts(User $user, OperationalSnapshot $s): array
    {
        $a = [];
        if ($user->can('leave.view')) {
            if ($s->pendingLeaveManager > 0) {
                $a[] = new OperationalAlertItem(
                    id: 'leave_manager',
                    title: __('dashboard.action_center.alerts.pending_manager_title'),
                    body: __('dashboard.action_center.alerts.pending_manager_body', ['count' => $s->pendingLeaveManager]),
                    severity: AlertSeverity::WARNING,
                    priority: 55,
                    actionUrl: Route::has('hr.leave.requests.index') ? route('hr.leave.requests.index') : null,
                    actionLabel: __('dashboard.action_center.review'),
                );
            }
            if ($user->can('leave.approve.hr') && $s->pendingLeaveHr > 0) {
                $a[] = new OperationalAlertItem(
                    id: 'leave_hr',
                    title: __('dashboard.action_center.alerts.pending_hr_title'),
                    body: __('dashboard.action_center.alerts.pending_hr_body', ['count' => $s->pendingLeaveHr]),
                    severity: AlertSeverity::DANGER,
                    priority: 65,
                    actionUrl: Route::has('hr.leave.requests.index') ? route('hr.leave.requests.index') : null,
                    actionLabel: __('dashboard.action_center.review'),
                );
            }
        }
        if ($user->can('attendance.view')) {
            if ($s->criticalAttendanceExceptions > 0) {
                $a[] = new OperationalAlertItem(
                    id: 'attn_critical',
                    title: __('dashboard.action_center.alerts.exceptions_critical_title'),
                    body: __('dashboard.action_center.alerts.exceptions_critical_body', ['count' => $s->criticalAttendanceExceptions]),
                    severity: AlertSeverity::CRITICAL,
                    priority: 72,
                    actionUrl: Route::has('hr.attendance.exceptions.index') ? route('hr.attendance.exceptions.index') : null,
                    actionLabel: __('dashboard.action_center.open_exceptions'),
                );
            }
            if ($s->pendingAttendanceExceptions > 0) {
                $a[] = new OperationalAlertItem(
                    id: 'attn_pending',
                    title: __('dashboard.action_center.alerts.exceptions_pending_title'),
                    body: __('dashboard.action_center.alerts.exceptions_pending_body', ['count' => $s->pendingAttendanceExceptions]),
                    severity: AlertSeverity::WARNING,
                    priority: 52,
                    actionUrl: Route::has('hr.attendance.exceptions.index') ? route('hr.attendance.exceptions.index') : null,
                    actionLabel: __('dashboard.action_center.open_exceptions'),
                );
            }
        }

        return $a;
    }

    /**
     * @return list<OperationalAlertItem>
     */
    private function billingAlerts(int $billingIssues, int $planRisks): array
    {
        $a = [];
        if ($billingIssues > 0) {
            $a[] = new OperationalAlertItem(
                id: 'billing',
                title: __('dashboard.action_center.alerts.subscription_title'),
                body: __('dashboard.action_center.alerts.subscription_body'),
                severity: AlertSeverity::CRITICAL,
                priority: 90,
                actionUrl: route('dashboard.index').'#dashboard-modules',
                actionLabel: __('dashboard.upgrade_plan'),
            );
        }
        if ($planRisks > 0) {
            $a[] = new OperationalAlertItem(
                id: 'plan_limits',
                title: __('dashboard.action_center.alerts.plan_limits_title'),
                body: __('dashboard.action_center.alerts.plan_limits_body'),
                severity: AlertSeverity::DANGER,
                priority: 70,
                actionUrl: route('dashboard.index').'#dashboard-modules',
                actionLabel: __('dashboard.upgrade_plan'),
            );
        }

        return $a;
    }
}
