<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Auth;
use App\Core\Request;
use App\Services\DashboardService;
use App\Services\SmartTipsService;

final class DashboardController extends BaseController
{
    public function index(Request $request): void
    {
        $this->requireModule('dashboard');
        $uid = (int) Auth::instance()->id();
        $n = $this->notifications();
        $tipsOn = SmartTipsService::enabled();
        if (has_role('manager')) {
            $askQ = trim((string) $request->input('ask', ''));
            view('dashboard/manager', [
                'title' => 'Dashboard',
                'data' => DashboardService::manager(),
                'tips' => $tipsOn ? SmartTipsService::manager() : [],
                'ask' => $tipsOn ? SmartTipsService::ask($askQ) : null,
                'notifications' => $n,
            ]);
            return;
        }
        if (has_role('service')) {
            view('dashboard/service', [
                'title' => 'Dashboard',
                'data' => DashboardService::service($uid),
                'tips' => $tipsOn ? SmartTipsService::service($uid) : [],
                'notifications' => $n,
            ]);
            return;
        }
        if (has_role('salesperson')) {
            view('dashboard/salesperson', [
                'title' => 'Dashboard',
                'data' => DashboardService::salesperson($uid),
                'tips' => $tipsOn ? SmartTipsService::salesperson($uid) : [],
                'notifications' => $n,
            ]);
            return;
        }
        if (has_role('admin')) {
            view('dashboard/admin', [
                'title' => 'Dashboard',
                'data' => DashboardService::admin(),
                'notifications' => $n,
            ]);
            return;
        }
        if (has_role('marketing')) {
            view('dashboard/marketing', [
                'title' => 'Dashboard',
                'data' => DashboardService::marketing(),
                'tips' => $tipsOn ? SmartTipsService::marketing() : [],
                'notifications' => $n,
            ]);
            return;
        }
        view('dashboard/empty', [
            'title' => 'Dashboard',
            'message' => 'Ask an admin to add a role so you can see your sections.',
            'notifications' => $n,
        ]);
    }
}
