<?php
/**
 * author/auth_bridge.php
 * Handles switching from reviewer dashboard → author dashboard.
 * Since both folders share the same PHP session (same PHPSESSID, path='/'),
 * the user_id is already in session — we just verify and redirect.
 */
error_reporting(0);
require_once __DIR__ . '/config/db.php';
startSession();

// Already have a valid session
if (!empty($_SESSION['user_id'])) {
    $uid = (int)$_SESSION['user_id'];
    try {
        $stmt = db()->prepare(
            "SELECT role FROM users WHERE user_id = ? AND is_active = 1 LIMIT 1"
        );
        $stmt->execute([$uid]);
        $row = $stmt->fetch();

        if ($row && in_array($row['role'], ['author', 'both'])) {
            // Valid — go to author dashboard
            header('Location: /author/dashboard.php');
            exit;
        }
    } catch (PDOException $e) { /* fall through */ }
}

// No session or not an author — send to author login
header('Location: /author/login.php?msg=session_expired');
exit;