<?php

namespace App\Core;

trait WorkflowStateTrait
{
    protected function calculateWorkflowStage(
        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';
    }
}
