<?php
namespace App\Models;

use App\Core\Database;

class Conference
{
    protected $db;

    public function __construct()
    {
        $this->db = Database::getInstance();
    }

    /* ===============================
       LIST CONFERENCES (ORG SAFE)
    =============================== */
    public function all()
    {
        if (function_exists('is_superadmin') && is_superadmin()) {
            return $this->db->query("SELECT * FROM conferences ORDER BY id DESC")->fetchAll();
        }

        $orgId = current_org_id();
        $stmt = $this->db->prepare("SELECT * FROM conferences WHERE org_id = ? ORDER BY id DESC");
        $stmt->execute([$orgId]);

        return $stmt->fetchAll();
    }

    /* ===============================
       FIND CONFERENCE (ORG SAFE)
    =============================== */
    public function find($id)
    {
        if (function_exists('is_superadmin') && is_superadmin()) {
            $stmt = $this->db->prepare("SELECT * FROM conferences WHERE id = ?");
            $stmt->execute([$id]);
            return $stmt->fetch();
        }

        $orgId = current_org_id();
        $stmt = $this->db->prepare("SELECT * FROM conferences WHERE id = ? AND org_id = ?");
        $stmt->execute([$id, $orgId]);

        return $stmt->fetch();
    }

    /* ===============================
       CREATE CONFERENCE
    =============================== */
    public function create($data)
    {
        $stmt = $this->db->prepare("
            INSERT INTO conferences (org_id, title, start_date, end_date)
            VALUES (?, ?, ?, ?)
        ");

        return $stmt->execute([
            current_org_id(),
            $data['title'],
            $data['start_date'],
            $data['end_date']
        ]);
    }
}
