from dataclasses import dataclass

from apps.projects.models import ProjectStage


STAGE_ORDER = [
    ProjectStage.Stage.SCRIPT,
    ProjectStage.Stage.BASE_ASSETS,
    ProjectStage.Stage.STORYBOARD,
    ProjectStage.Stage.VIDEO,
    ProjectStage.Stage.EXPORT,
]


@dataclass(frozen=True)
class StageTransition:
    current: str
    target: str
    allowed: bool
    reason: str = ""


def can_enter_stage(current_stage: str, target_stage: str, allow_skip_storyboard: bool = True) -> StageTransition:
    if target_stage not in STAGE_ORDER:
        return StageTransition(current_stage, target_stage, False, "unknown target stage")

    current_index = STAGE_ORDER.index(current_stage) if current_stage in STAGE_ORDER else -1
    target_index = STAGE_ORDER.index(target_stage)

    if target_index <= current_index + 1:
        return StageTransition(current_stage, target_stage, True)

    if allow_skip_storyboard and current_stage == ProjectStage.Stage.BASE_ASSETS and target_stage == ProjectStage.Stage.VIDEO:
        return StageTransition(current_stage, target_stage, True)

    return StageTransition(current_stage, target_stage, False, "stage prerequisite is not satisfied")

