<?php

namespace App\Models;

use App\Core\Model;

class Event extends Model
{
    protected $table = 'events';
    protected $primaryKey = 'id';

    public function getUpcoming($limit = 10)
    {
        $stmt = $this->db->prepare("SELECT e.*, u.name as creator_name FROM {$this->table} e LEFT JOIN users u ON e.created_by = u.id WHERE e.status IN ('upcoming','ongoing') AND e.start_datetime >= NOW() ORDER BY e.start_datetime ASC LIMIT ?");
        $stmt->execute([$limit]);
        return $stmt->fetchAll(\PDO::FETCH_OBJ);
    }

    public function getWithCreator($id)
    {
        $stmt = $this->db->prepare("SELECT e.*, u.name as creator_name FROM {$this->table} e LEFT JOIN users u ON e.created_by = u.id WHERE e.id = ?");
        $stmt->execute([$id]);
        return $stmt->fetch(\PDO::FETCH_OBJ);
    }

    public function getPublicUpcoming($limit = 10)
    {
        $stmt = $this->db->prepare("SELECT e.*, u.name as creator_name FROM {$this->table} e LEFT JOIN users u ON e.created_by = u.id WHERE e.status IN ('upcoming','ongoing') AND e.start_datetime >= NOW() AND e.visibility = 'public' ORDER BY e.start_datetime ASC LIMIT ?");
        $stmt->execute([$limit]);
        return $stmt->fetchAll(\PDO::FETCH_OBJ);
    }

    public function getAttendeeCount($eventId)
    {
        $stmt = $this->db->prepare("SELECT COUNT(*) as count FROM event_attendance WHERE event_id = ? AND status = 'registered'");
        $stmt->execute([$eventId]);
        return $stmt->fetch(\PDO::FETCH_OBJ)->count;
    }
}
