<?php

namespace App\Services\Frontend;

use App\Models\User;

class LandingPageService
{
    public function __construct(
        private LandingPageRoleService $roleService,
        private LandingPageContextService $contextService,
        private LandingPageTeacherSummaryService $teacherSummaryService,
        private LandingPageParentDashboardService $parentDashboardService
    ) {
    }

    public function dashboardForUser(?User $user, ?int $classSectionId = null): array
    {
        $role = $this->roleService->resolveRole($user);

        return match ($role) {
            'administrator' => $this->administrator(),
            'admin' => $this->admin(),
            'teacher', 'teacher_assistant' => $this->teacher($user, $classSectionId),
            'student' => $this->student(),
            'parent', 'authorized_user' => $this->parent($user),
            default => $this->guest(),
        };
    }

    public function administrator(): array
    {
        return [
            'role' => 'administrator',
            'dashboard' => 'administrator',
        ];
    }

    public function admin(): array
    {
        return [
            'role' => 'admin',
            'dashboard' => 'admin',
        ];
    }

    public function teacher(?User $user, ?int $classSectionId = null): array
    {
        if (!$user) {
            return ['role' => 'teacher', 'dashboard' => 'teacher', 'summary' => []];
        }

        return [
            'role' => 'teacher',
            'dashboard' => 'teacher',
            'summary' => $this->teacherSummaryService->summary($user->id, $classSectionId),
        ];
    }

    public function student(): array
    {
        return [
            'role' => 'student',
            'dashboard' => 'student',
        ];
    }

    public function parent(?User $user): array
    {
        if (!$user) {
            return ['role' => 'parent', 'dashboard' => 'parent', 'summary' => []];
        }

        $userType = (string) ($user->user_type ?? 'primary');

        return [
            'role' => 'parent',
            'dashboard' => 'parent',
            'summary' => $this->parentDashboardService->summary($user->id, $userType),
        ];
    }

    public function guest(): array
    {
        return [
            'role' => 'guest',
            'dashboard' => 'guest',
        ];
    }
}
