# -*- coding: utf-8 -*-
"""
STEP103-18 ResumeStrategy Test

실행:
  cd /d D:/honghee/blog_api
  python -m tools.test_v2_resume_strategy

목적:
- ResumeInspector 결과 stage에 따라 ResumeStrategy가 올바른 action을 반환하는지 확인한다.
"""

import sys
from dataclasses import dataclass
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

from services.v2.resume_strategy import ResumeStrategy


@dataclass
class FakeInspection:
    should_resume: bool
    reason: str
    pipeline_id: str = "V2-RESUME-STRATEGY-TEST"
    realtor_id: int = 1
    stage: str | None = None
    article_no: str | None = "2633339599"
    draft_id: int | None = 256
    publish_queue_id: int | None = 157


def run_case(strategy, name, inspection, expected_action, expected_resume_from):
    result = strategy.decide(inspection)

    print("-" * 100)
    print(f"[{name}]")
    print("inspection_stage:", inspection.stage if inspection else None)
    print("should_resume:", result.should_resume)
    print("action:", result.action)
    print("resume_from:", result.resume_from)
    print("message:", result.message)

    if result.action != expected_action:
        raise SystemExit(
            f"FAILED {name}: expected action={expected_action}, got={result.action}"
        )

    if result.resume_from != expected_resume_from:
        raise SystemExit(
            f"FAILED {name}: expected resume_from={expected_resume_from}, got={result.resume_from}"
        )


def main():
    strategy = ResumeStrategy()

    print("=" * 100)
    print("[STEP103-18 RESUME STRATEGY TEST]")

    run_case(
        strategy,
        "no_resume",
        FakeInspection(should_resume=False, reason="last_pipeline_finished_success", stage="pipeline"),
        expected_action="none",
        expected_resume_from=None,
    )

    run_case(
        strategy,
        "pipeline",
        FakeInspection(should_resume=True, reason="pipeline_interrupted", stage="pipeline"),
        expected_action="full_pipeline",
        expected_resume_from="session",
    )

    run_case(
        strategy,
        "session",
        FakeInspection(should_resume=True, reason="session_interrupted", stage="session"),
        expected_action="collect_and_after",
        expected_resume_from="collect",
    )

    run_case(
        strategy,
        "collect",
        FakeInspection(should_resume=True, reason="collect_interrupted", stage="collect"),
        expected_action="draft_and_after",
        expected_resume_from="draft",
    )

    run_case(
        strategy,
        "draft",
        FakeInspection(should_resume=True, reason="draft_interrupted", stage="draft"),
        expected_action="publish_and_after",
        expected_resume_from="publish",
    )

    run_case(
        strategy,
        "publish",
        FakeInspection(should_resume=True, reason="publish_interrupted", stage="publish"),
        expected_action="publish_retry",
        expected_resume_from="publish",
    )

    run_case(
        strategy,
        "unknown",
        FakeInspection(should_resume=True, reason="unknown_stage", stage="unknown"),
        expected_action="full_pipeline",
        expected_resume_from="session",
    )

    print("-" * 100)
    print("SUCCESS")
    print("=" * 100)


if __name__ == "__main__":
    main()
