<?php

declare(strict_types=1);

namespace App\Repositories;

final class WorkTimesheetRepository extends BaseRepository
{
    /**
     * Hole alle Appointments eines Fahrlehrers im Zeitraum mit lesson_type Info.
     * Alle Categories zählen als Arbeitszeit.
     */
    public function getAppointmentsForInstructor(int $tenantId, int $instructorId, string $from, string $to): array
    {
        $sql = 'SELECT
                    a.id,
                    a.start_at,
                    a.end_at,
                    a.units,
                    lt.name AS lesson_type_name,
                    lt.category AS lesson_type_category,
                    lt.default_duration
                FROM appointments a
                JOIN lesson_types lt ON lt.id = a.lesson_type_id
                WHERE a.tenant_id = :tenant_id
                  AND a.instructor_id = :instructor_id
                  AND a.start_at >= :from
                  AND a.start_at < :to
                  AND a.status != \'cancelled\'
                  AND lt.category != \'private\'
                ORDER BY a.start_at ASC';

        $statement = $this->db->prepare($sql);
        $statement->execute([
            'tenant_id' => $tenantId,
            'instructor_id' => $instructorId,
            'from' => $from . ' 00:00:00',
            'to' => $to . ' 23:59:59',
        ]);

        return $statement->fetchAll();
    }

    public function getInstructor(int $tenantId, int $instructorId): ?array
    {
        $statement = $this->db->prepare(
            'SELECT id, first_name, last_name FROM instructors WHERE tenant_id = :tenant_id AND id = :id'
        );
        $statement->execute(['tenant_id' => $tenantId, 'id' => $instructorId]);

        return $statement->fetch() ?: null;
    }
}