<?php
namespace App\Controllers;

use App\Core\Database;

class ProceedingsController
{
    public function index()
    {
        $db = Database::getInstance();

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

        if ($conferenceId <= 0 && !empty($_SESSION['conference_id'])) {
            $conferenceId = (int)$_SESSION['conference_id'];
        }

        if ($conferenceId <= 0) {
            http_response_code(404);
            exit("Conference not selected.");
        }

        $stmt = $db->prepare("
            SELECT *
            FROM conferences
            WHERE id = ?
            LIMIT 1
        ");
        $stmt->execute([$conferenceId]);
        $conference = $stmt->fetch(\PDO::FETCH_ASSOC);

        if (!$conference) {
            http_response_code(404);
            exit("Conference not found.");
        }

        $stmt = $db->prepare("
            SELECT
                id,
                submission_no,
                COALESCE(title, title_en, title_ar, CONCAT('Submission #', id)) AS title,
                COALESCE(author_name, '') AS author_name,
                abstract,
                abstract_ar,
                keywords,
                keywords_ar,
                doi,
                published_at,
                file_path,
                status,
                stage
            FROM submissions
            WHERE conference_id = ?
              AND status IN ('accepted','galley','assigned_to_issue','production','published')
            ORDER BY published_at DESC, id DESC
        ");
        $stmt->execute([$conferenceId]);
        $papers = $stmt->fetchAll(\PDO::FETCH_ASSOC);

        require BASE_PATH . '/app/views/proceedings/index.php';
    }
}
