<?php

namespace App\Services;

/**
 * ------------------------------------------------------------
 * Enterprise Workflow Dictionary
 * Pack 2209
 *
 * Single source of truth for workflow terminology.
 * This class contains NO database code.
 * ------------------------------------------------------------
 */
final class WorkflowDictionary
{
    /*
    ------------------------------------------------------------
    Enterprise Pack 2410
    Canonical workflow constants
    ------------------------------------------------------------
    */

    public const STATUS_SUBMITTED         = 'submitted';
    public const STATUS_SCREENING         = 'screening';
    public const STATUS_UNDER_REVIEW      = 'under_review';
    public const STATUS_REVISION_REQUIRED = 'revision_required';
    public const STATUS_ACCEPTED          = 'accepted';
    public const STATUS_REJECTED          = 'rejected';
    public const STATUS_PRODUCTION        = 'production';
    public const STATUS_ASSIGNED_TO_ISSUE = 'assigned_to_issue';
    public const STATUS_PUBLISHED         = 'published';

    public const STAGE_SUBMISSION = 'submission';
    public const STAGE_SCREENING  = 'screening';
    public const STAGE_REVIEW     = 'review';
    public const STAGE_REVISION   = 'revision';
    public const STAGE_DECISION   = 'decision';
    public const STAGE_PRODUCTION = 'production';
    public const STAGE_ISSUE      = 'issue';
    public const STAGE_PUBLISHED  = 'published';

    /*
    ------------------------------------------------------------
    Canonical Workflow Stages
    ------------------------------------------------------------
    */

    public const STAGES = [

        self::STAGE_SUBMISSION,

        self::STAGE_SCREENING,

        self::STAGE_REVIEW,

        self::STAGE_REVISION,

        self::STAGE_DECISION,

        self::STAGE_PRODUCTION,

        self::STAGE_ISSUE,

        self::STAGE_PUBLISHED

    ];

    /*
    ------------------------------------------------------------
    Business Status
    ------------------------------------------------------------
    */

    public const STATUS = [

        self::STATUS_SUBMITTED,

        self::STATUS_SCREENING,

        self::STATUS_UNDER_REVIEW,

        self::STATUS_REVISION_REQUIRED,

        self::STATUS_ACCEPTED,

        self::STATUS_REJECTED,

        self::STATUS_PRODUCTION,

        self::STATUS_ASSIGNED_TO_ISSUE,

        self::STATUS_PUBLISHED

    ];

    /*
    ------------------------------------------------------------
    Production Status
    ------------------------------------------------------------
    */

    public const PRODUCTION_STATUS = [

        'pending',

        'camera_ready_uploaded',

        'under_production',

        'proofreading',

        'galley_ready',

        'approved',

        'published'

    ];

    /*
    ------------------------------------------------------------
    Helpers
    ------------------------------------------------------------
    */

    public static function isStage(string $stage): bool
    {
        return in_array($stage, self::STAGES, true);
    }

    public static function isStatus(string $status): bool
    {
        return in_array($status, self::STATUS, true);
    }

    public static function isProductionStatus(string $status): bool
    {
        return in_array($status, self::PRODUCTION_STATUS, true);
    }

    /*
    ------------------------------------------------------------
    workflow_stage mirrors stage
    ------------------------------------------------------------
    */

    public static function workflowStage(string $stage): string
    {
        return self::isStage($stage)
            ? $stage
            : 'submission';
    }

    /*
    ------------------------------------------------------------
    Production helper
    ------------------------------------------------------------
    */

    public static function isProductionStage(string $stage): bool
    {
        return in_array(
            $stage,
            [
                'production',
                'issue',
                'published'
            ],
            true
        );
    }

    /*
    ------------------------------------------------------------
    Review helper
    ------------------------------------------------------------
    */

    public static function isReviewStage(string $stage): bool
    {
        return in_array(
            $stage,
            [
                'review',
                'revision',
                'decision'
            ],
            true
        );
    }

    /*
    ------------------------------------------------------------
    Terminal helper
    ------------------------------------------------------------
    */

    public static function isTerminalStatus(string $status): bool
    {
        return in_array(
            $status,
            [
                'accepted',
                'rejected',
                'published'
            ],
            true
        );
    }
    
    /*
------------------------------------------------------------
Legacy workflow mapping
Matches current production behaviour exactly
------------------------------------------------------------
*/

public static function workflowStageFromStatus(
    string $status,
    string $stage
): string
{
    if (
        $stage === 'decision'
        || $status === 'screening'
    ) {
        return 'decision';
    }

    if (
        $status === 'under_review'
        || in_array($stage, ['review','reviewing'], true)
    ) {
        return 'under_review';
    }

    if (
        $status === 'revision_required'
        || in_array($stage, ['revision','revision_required'], true)
    ) {
        return 'revision_required';
    }

    if (
        $status === 'accepted'
        || $stage === 'accepted'
    ) {
        return 'accepted';
    }

    if (
        $status === 'rejected'
        || $stage === 'rejected'
    ) {
        return 'rejected';
    }

    if (
        in_array(
            $status,
            [
                'production',
                'galley',
                'assigned_to_issue'
            ],
            true
        )
        ||
        in_array(
            $stage,
            [
                'production',
                'galley',
                'issue',
                'assigned_to_issue'
            ],
            true
        )
    ) {
        return 'production';
    }

    if (
        $status === 'published'
        || $stage === 'published'
    ) {
        return 'published';
    }

    return 'submitted';
}
}
