<?php

declare(strict_types=1);

namespace App\Services;

use App\Repositories\AppointmentRepository;
use App\Repositories\StudentRepository;
use App\Support\Database;

final class RequirementService
{
    public function progressForStudent(int $studentId): array
    {
        $db = Database::connection();
        $snapshots = (new StudentRepository())->requirements($studentId);

        $statement = $db->prepare(
            'SELECT au.requirement_key,
                    SUM(CASE WHEN a.status = "planned" THEN au.units_counted ELSE 0 END) AS planned_units,
                    SUM(CASE WHEN a.status = "completed" THEN au.units_counted ELSE 0 END) AS completed_units
             FROM appointment_units au
             JOIN appointments a ON a.id = au.appointment_id
             WHERE a.student_id = :student_id
             GROUP BY au.requirement_key'
        );
        $statement->execute(['student_id' => $studentId]);
        $totals = [];
        foreach ($statement->fetchAll() as $row) {
            $totals[$row['requirement_key']] = [
                'planned' => (int) $row['planned_units'],
                'completed' => (int) $row['completed_units'],
            ];
        }

        return array_map(static function (array $snapshot) use ($totals): array {
            $row = $totals[$snapshot['requirement_key']] ?? ['planned' => 0, 'completed' => 0];
            $priorCompleted = (int) ($snapshot['prior_completed_units'] ?? 0);
            $appointmentCompleted = (int) $row['completed'];
            $completed = $priorCompleted + $appointmentCompleted;
            $open = (int) $snapshot['required_units'] - $row['planned'] - $completed;

            return [
                'requirement_key' => $snapshot['requirement_key'],
                'phase_label' => $snapshot['phase_label'] ?? '',
                'label' => $snapshot['label'],
                'required_units' => (int) $snapshot['required_units'],
                'planned_units' => $row['planned'],
                'completed_units' => $completed,
                'prior_completed_units' => $priorCompleted,
                'appointment_completed_units' => $appointmentCompleted,
                'open_units' => $open,
            ];
        }, $snapshots);
    }
}
