<?php
namespace App\Core;

use App\Core\Database;
use App\Core\MailEngine;

class WorkflowEngine
{
    /* --------------------------------------------------
       STAGE MACHINE
    -------------------------------------------------- */

    public static function advanceStage($submission_id)
    {
        $db = Database::getInstance();

        $stmt = $db->prepare("
            SELECT stage FROM submissions WHERE id = ?
        ");
        $stmt->execute([$submission_id]);
        $stage = $stmt->fetchColumn();

        $next = [
            'submitted' => 'screening',
            'screening' => 'under_review',
            'under_review' => 'revision_required',
            'revision_required' => 'revised',
            'revised' => 'under_review',
            'accepted' => 'galley',
            'galley' => 'production',
            'production' => 'published'
        ];

        if (isset($next[$stage])) {

            $update = $db->prepare("
                UPDATE submissions SET stage=? WHERE id=?
            ");
            $update->execute([$next[$stage], $submission_id]);

            // If published → auto DOI
            if ($next[$stage] === 'published') {
                self::generateDOI($submission_id);
            }
        }
    }


    /* --------------------------------------------------
       DECISION ENGINE
    -------------------------------------------------- */

public static function setDecision($submission_id, $decision)
{
    $db = Database::getInstance();

    if ($decision === 'revision_required') {

        $db->prepare("
            UPDATE submissions
            SET decision = ?,
                stage = 'revision_required',
                revision_open_until = DATE_ADD(NOW(), INTERVAL 14 DAY)
            WHERE id = ?
        ")->execute([$decision, $submission_id]);

        return;
    }

    $db->prepare("
        UPDATE submissions
        SET decision = ?, stage = ?
        WHERE id = ?
    ")->execute([$decision, $decision, $submission_id]);
}
    /* --------------------------------------------------
       AUTO DECISION RECOMMENDATION
    -------------------------------------------------- */

    public static function autoRecommendDecision($submission_id)
    {
        $db = Database::getInstance();

        $score = $db->query("
            SELECT weighted_average
            FROM v_submission_weighted_scores
            WHERE submission_id = " . intval($submission_id)
        )->fetchColumn();

        if (!$score) return null;

        if ($score >= 4) return 'accepted';
        if ($score >= 3) return 'minor_revision';
        if ($score >= 2) return 'major_revision';
        return 'rejected';
    }


    /* --------------------------------------------------
       DOI GENERATOR
    -------------------------------------------------- */

    public static function generateDOI($submission_id)
    {
        $db = Database::getInstance();

        $prefix = "10.5678/conf";

        $doi = $prefix . "." . $submission_id;

        $db->prepare("
            UPDATE submissions
            SET doi=?
            WHERE id=?
        ")->execute([$doi, $submission_id]);

        return $doi;
    }


    /* --------------------------------------------------
       EMAIL TRIGGER
    -------------------------------------------------- */

    private static function notifyAuthor($submission_id, $decision)
    {
        $db = Database::getInstance();

        $stmt = $db->prepare("
            SELECT u.email, s.title
            FROM submissions s
            JOIN users u ON u.id = s.author_id
            WHERE s.id = ?
        ");
        $stmt->execute([$submission_id]);

        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
        if (!$row) return;

        MailEngine::sendTemplate(
            'decision_notification',
            $row['email'],
            [
                'TITLE' => $row['title'],
                'DECISION' => strtoupper($decision)
            ]
        );
    }
}
