<?php

declare(strict_types=1);

namespace App\Repositories;

final class PrivateAppointmentRepository extends BaseRepository
{
    public function getForInstructor(int $instructorId): array
    {
        $stmt = $this->db->prepare(
            'SELECT * FROM private_appointments
             WHERE instructor_id = :instructor_id
             ORDER BY date ASC, time_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 private_appointments
             WHERE instructor_id = :instructor_id
               AND date >= :from AND date <= :to
             ORDER BY date ASC, time_from ASC'
        );
        $stmt->execute(['instructor_id' => $instructorId, 'from' => $from, 'to' => $to]);
        return $stmt->fetchAll();
    }

    public function create(int $instructorId, string $title, string $date, string $timeFrom, string $timeTo, string $color = '#9ca3af'): array
    {
        $stmt = $this->db->prepare(
            'INSERT INTO private_appointments (instructor_id, title, date, time_from, time_to, color)
             VALUES (:instructor_id, :title, :date, :time_from, :time_to, :color)
             RETURNING *'
        );
        $stmt->execute([
            'instructor_id' => $instructorId,
            'title' => $title,
            'date' => $date,
            'time_from' => $timeFrom,
            'time_to' => $timeTo,
            'color' => $color,
        ]);
        return $stmt->fetch();
    }

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