<?php
namespace App\Controllers;

require_once BASE_PATH . '/app/helpers/permissions.php';

require_once BASE_PATH . '/app/helpers/conference_scope.php';

use App\Core\Database;

class NotificationController
{
    protected function db()
    {
        return Database::getInstance();
    }

    private function startSession(): void
    {
        if (session_status() !== PHP_SESSION_ACTIVE) {
            session_start();
        }
    }

    private function requireLogin(): void
    {
        $this->startSession();

        if (empty($_SESSION['user_id'])) {
            header("Location: /easychair/login");
            exit;
        }
    }

    public function index()
    {
$this->requireLogin();

        $status = trim((string)($_GET['status'] ?? ''));

        $where = "WHERE user_id=?";
        $params = [(int)$_SESSION['user_id']];

        if ($status === 'unread') {
            $where .= " AND is_read=0";
        } elseif ($status === 'read') {
            $where .= " AND is_read=1";
        }

        $stmt = $this->db()->prepare("
            SELECT n.*, s.submission_no, COALESCE(s.title,s.title_en,s.title_ar) AS submission_title,
                   c.short_name AS conference_short_name
            FROM user_notifications n
            LEFT JOIN submissions s ON s.id=n.submission_id
            LEFT JOIN conferences c ON c.id=n.conference_id
            {$where}
            ORDER BY n.id DESC
            LIMIT 300
        ");
        $stmt->execute($params);
        $notifications = $stmt->fetchAll(\PDO::FETCH_ASSOC);

        ob_start();
        require BASE_PATH . '/app/views/notifications/index.php';
        $content = ob_get_clean();

        $role = $_SESSION['conference_role'] ?? $_SESSION['role'] ?? $_SESSION['user_role'] ?? '';

        if (in_array($role, ['superadmin','org_admin','admin','organizer','conference_chair','track_chair'], true)) {
            require BASE_PATH . '/app/views/layouts/admin_layout.php';
        } elseif ($role === 'reviewer') {
            require BASE_PATH . '/app/views/layouts/reviewer_layout.php';
        } else {
            require BASE_PATH . '/app/views/layouts/author_layout.php';
        }
    }

    public function markRead()
    {
        $this->requireLogin();

        $id = (int)($_GET['id'] ?? 0);

        $stmt = $this->db()->prepare("
            UPDATE user_notifications
            SET is_read=1,
                read_at=NOW()
            WHERE id=?
              AND user_id=?
        ");
        $stmt->execute([$id, (int)$_SESSION['user_id']]);

        header("Location: /easychair/notifications");
        exit;
    }

    public function markAllRead()
    {
        $this->requireLogin();

        $stmt = $this->db()->prepare("
            UPDATE user_notifications
            SET is_read=1,
                read_at=NOW()
            WHERE user_id=?
              AND is_read=0
        ");
        $stmt->execute([(int)$_SESSION['user_id']]);

        header("Location: /easychair/notifications");
        exit;
    }
}
