<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Auth;
use App\Core\Database;
use App\Core\Response;
use App\Core\View;
use App\Services\DashboardService;

final class HomeController
{
    public function index(): Response
    {
        $therapistId = null;
        $isTherapist = Auth::hasRole('THERAPIST') && !Auth::hasRole('ADMIN') && !Auth::hasRole('SUPER_ADMIN');
        if ($isTherapist) {
            $stmt = Database::connection()->prepare('SELECT id FROM therapists WHERE user_id=:user_id AND status="active" LIMIT 1');
            $stmt->execute(['user_id' => Auth::id()]);
            $therapistId = ($id = $stmt->fetchColumn()) !== false ? (int)$id : null;
        }

        $data = (new DashboardService())->overview($therapistId);
        $view = new View(dirname(__DIR__, 2) . '/resources/views');
        return Response::html($view->render('dashboard/index', [
            'pageTitle' => 'Dashboard',
            'breadcrumbs' => ['Dashboard'],
            'dashboard' => $data,
            'isTherapistDashboard' => $isTherapist,
        ]));
    }
}
