<?php

namespace App\Models;

use App\Core\Model;

class CalendarEvent extends Model
{
    protected $table = 'calendar_events';
    protected $primaryKey = 'id';

    public function getByUser($userId)
    {
        $stmt = $this->db->prepare("SELECT * FROM {$this->table} WHERE user_id = ? ORDER BY start_date ASC");
        $stmt->execute([$userId]);
        return $stmt->fetchAll(\PDO::FETCH_OBJ);
    }

    public function getByDateRange($start, $end)
    {
        $stmt = $this->db->prepare("
            SELECT c.*, u.name as user_name
            FROM {$this->table} c
            LEFT JOIN users u ON c.user_id = u.id
            WHERE c.start_date >= ? AND c.start_date <= ?
            ORDER BY c.start_date ASC
        ");
        $stmt->execute([$start, $end]);
        return $stmt->fetchAll(\PDO::FETCH_OBJ);
    }

    public function getUpcoming($limit = 5)
    {
        $stmt = $this->db->prepare("
            SELECT c.*, u.name as user_name
            FROM {$this->table} c
            LEFT JOIN users u ON c.user_id = u.id
            WHERE c.start_date >= NOW()
            ORDER BY c.start_date ASC LIMIT ?
        ");
        $stmt->execute([$limit]);
        return $stmt->fetchAll(\PDO::FETCH_OBJ);
    }
}
