<?php
class TheoryLesson
{
    public static function all(bool $includeInactive = true): array
    {
        Database::migrate();
        $sql = 'SELECT * FROM theoretical_lessons';
        if (!$includeInactive) {
            $sql .= ' WHERE is_active = 1';
        }
        $sql .= ' ORDER BY lesson_date ASC, start_time ASC';
        return Database::pdo()->query($sql)->fetchAll();
    }

    public static function upcoming(): array
    {
        Database::migrate();
        $stmt = Database::pdo()->prepare("SELECT * FROM theoretical_lessons WHERE is_active = 1 AND lesson_date >= ? ORDER BY lesson_date ASC, start_time ASC");
        $stmt->execute([date('Y-m-d')]);
        return $stmt->fetchAll();
    }

    public static function save(array $data): array
    {
        Database::migrate();
        $id = (int)($data['id'] ?? 0);
        $title = trim($data['title'] ?? 'Aula teórica');
        $lessonDate = trim($data['lesson_date'] ?? '');
        $startTime = trim($data['start_time'] ?? '');
        $endTime = trim($data['end_time'] ?? '');
        $courseDuration = trim($data['course_duration'] ?? '');
        $subject = trim($data['subject'] ?? '');
        $notes = trim($data['notes'] ?? '');
        $isActive = !empty($data['is_active']) ? 1 : 0;

        if ($title === '' || $lessonDate === '' || $startTime === '') {
            return ['ok' => false, 'message' => 'Informe titulo, data e horário inicial da aula teórica.'];
        }
        if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $lessonDate) || !preg_match('/^\d{2}:\d{2}$/', $startTime)) {
            return ['ok' => false, 'message' => 'Data ou horário invalido.'];
        }
        if ($endTime !== '' && !preg_match('/^\d{2}:\d{2}$/', $endTime)) {
            return ['ok' => false, 'message' => 'Horário final invalido.'];
        }
        $endTime = $endTime !== '' ? $endTime : null;

        $pdo = Database::pdo();
        if ($id > 0) {
            $stmt = $pdo->prepare('UPDATE theoretical_lessons SET title = ?, lesson_date = ?, start_time = ?, end_time = ?, course_duration = ?, subject = ?, notes = ?, is_active = ? WHERE id = ?');
            $stmt->execute([$title, $lessonDate, $startTime, $endTime, $courseDuration, $subject, $notes, $isActive, $id]);
            return ['ok' => true, 'message' => 'Aula teórica atualizada.'];
        }

        $stmt = $pdo->prepare('INSERT INTO theoretical_lessons (title, lesson_date, start_time, end_time, course_duration, subject, notes, is_active, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
        $stmt->execute([$title, $lessonDate, $startTime, $endTime, $courseDuration, $subject, $notes, $isActive, date('Y-m-d H:i:s')]);
        return ['ok' => true, 'message' => 'Aula teórica cadastrada.'];
    }

    public static function delete(int $id): bool
    {
        Database::migrate();
        $stmt = Database::pdo()->prepare('DELETE FROM theoretical_lessons WHERE id = ?');
        $stmt->execute([$id]);
        return $stmt->rowCount() > 0;
    }
}
