<?php

declare(strict_types=1);

namespace App\Repositories;

final class InstructorVacationRepository extends BaseRepository
{
    public function getForInstructor(int $instructorId): array
    {
        $stmt = $this->db->prepare(
            'SELECT * FROM instructor_vacations
             WHERE instructor_id = :instructor_id
             ORDER BY date_from ASC'
        );
        $stmt->execute(['instructor_id' => $instructorId]);
        return $stmt->fetchAll();
    }

    public function getForInstructorInRange(int $instructorId, string $from, string $to): array
    {
        $stmt = $this->db->prepare(
            'SELECT * FROM instructor_vacations
             WHERE instructor_id = :instructor_id
               AND date_from <= :to AND date_to >= :from
             ORDER BY date_from ASC'
        );
        $stmt->execute(['instructor_id' => $instructorId, 'from' => $from, 'to' => $to]);
        return $stmt->fetchAll();
    }

    public function create(int $instructorId, string $dateFrom, string $dateTo, string $notes = ''): array
    {
        $stmt = $this->db->prepare(
            'INSERT INTO instructor_vacations (instructor_id, date_from, date_to, notes)
             VALUES (:instructor_id, :date_from, :date_to, :notes)
             RETURNING *'
        );
        $stmt->execute([
            'instructor_id' => $instructorId,
            'date_from' => $dateFrom,
            'date_to' => $dateTo,
            'notes' => $notes,
        ]);
        return $stmt->fetch();
    }

    public function delete(int $id): bool
    {
        $stmt = $this->db->prepare('DELETE FROM instructor_vacations WHERE id = :id RETURNING id');
        $stmt->execute(['id' => $id]);
        return (bool) $stmt->fetch();
    }
}