<?php

namespace App\Models;

use App\Core\Model;

class EventAttendance extends Model
{
    protected $table = 'event_attendance';
    protected $primaryKey = 'id';

    public function getByEvent($eventId)
    {
        $stmt = $this->db->prepare("SELECT a.*, u.name as user_name, u.email as user_email FROM {$this->table} a LEFT JOIN users u ON a.user_id = u.id WHERE a.event_id = ? ORDER BY a.registered_at DESC");
        $stmt->execute([$eventId]);
        return $stmt->fetchAll(\PDO::FETCH_OBJ);
    }

    public function getByUser($userId)
    {
        $stmt = $this->db->prepare("SELECT a.*, e.title as event_title, e.start_datetime FROM {$this->table} a LEFT JOIN events e ON a.event_id = e.id WHERE a.user_id = ? ORDER BY e.start_datetime DESC");
        $stmt->execute([$userId]);
        return $stmt->fetchAll(\PDO::FETCH_OBJ);
    }

    public function isRegistered($eventId, $userId)
    {
        $stmt = $this->db->prepare("SELECT id FROM {$this->table} WHERE event_id = ? AND user_id = ?");
        $stmt->execute([$eventId, $userId]);
        return $stmt->fetch(\PDO::FETCH_OBJ) ? true : false;
    }
}
