<?php
declare(strict_types=1);

namespace App\Controllers;

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

use App\Core\Controller;
use App\Core\Database;
use App\Models\ProductionModel;
use App\Models\OperationsModel;

final class AuthorPortalController extends Controller
{
    public function dashboard(): void
    {
        require_login();

        $db = Database::getInstance();
        $authorId = (int) auth_id();

        $stmt = $db->prepare("
            SELECT s.*, c.title_en AS conference_title_en, c.title_ar AS conference_title_ar
            FROM submissions s
            LEFT JOIN conferences c ON c.id = s.conference_id
            WHERE s.author_id = :author_id
            ORDER BY s.id DESC
            LIMIT 200
        ");
        $stmt->execute([':author_id' => $authorId]);
        $rows = $stmt->fetchAll();

        $ops = new OperationsModel();
        $prod = new ProductionModel();

        $workflows = [];
        $rounds = [];
        $copyediting = [];
        $galleys = [];
        $doi = [];

        foreach ($rows as $r) {
            $sid = (int) $r['id'];
            $workflows[$sid] = $ops->workflowBySubmission($sid);
            $rounds[$sid] = $ops->roundRows($sid);
            $copyediting[$sid] = $prod->copyediting($sid);
            $galleys[$sid] = $prod->galleys($sid);
            $doi[$sid] = $prod->doi($sid);
        }

        $this->view('author/dashboard', [
            'rows' => $rows,
            'workflows' => $workflows,
            'rounds' => $rounds,
            'copyediting' => $copyediting,
            'galleys' => $galleys,
            'doi' => $doi,
        ]);
    }
}
