"""Example 1: Naming Conventions.

Demonstrates how to use ModelNameBuilder to generate standardized
MLflow experiment names, registered model names, and run names
following the TAT naming convention.

Run:
    python examples/01_naming_conventions.py
"""

from dgtw_mlflow_utils import (
    Architecture,
    ModelNameBuilder,
    ModelVariant,
    RunAction,
    Stage,
)


def main():
    """Main function to execute the example."""
    builder = ModelNameBuilder()

    # ------------------------------------------------------------------
    # 1) Experiment Names  →  TAT_{Component}_{Stage}
    # ------------------------------------------------------------------
    print("=" * 60)
    print("  Experiment Names")
    print("=" * 60)

    experiments = [
        ("Recommendation", Stage.RESEARCH),
        ("Recommendation", Stage.STAGING),
        ("Recommendation", Stage.PRODUCTION),
        ("Ranking", Stage.EXPERIMENT),
    ]

    for component, stage in experiments:
        name = builder.experiment_name(component=component, stage=stage)
        print(f"  {name}")

    # You can also pass a custom string instead of an enum
    custom = builder.experiment_name(component="ContextAware", stage="Beta")
    print(f"  {custom}  (custom stage)")

    # ------------------------------------------------------------------
    # 2) Registered Model Names  →  TAT_{Architecture}_{Variant}
    # ------------------------------------------------------------------
    print()
    print("=" * 60)
    print("  Registered Model Names")
    print("=" * 60)

    models = [
        (Architecture.TWO_TOWER, ModelVariant.BASELINE),
        (Architecture.TWO_TOWER, ModelVariant.CHAMPION),
        (Architecture.RANKING, ModelVariant.CHALLENGER),
        (Architecture.BERT4REC, ModelVariant.ABLATION),
        (Architecture.LIGHTGCN, ModelVariant.CANARY),
    ]

    for arch, variant in models:
        name = builder.registered_model_name(architecture=arch, variant=variant)
        print(f"  {name}")

    # ------------------------------------------------------------------
    # 3) Run Names  →  {Action}_{UseCase}_{Timestamp}
    # ------------------------------------------------------------------
    print()
    print("=" * 60)
    print("  Run Names")
    print("=" * 60)

    runs = [
        (RunAction.TRAIN, "MiniLM"),
        (RunAction.UPLOAD, "TwoTower"),
        (RunAction.EVALUATE, "Baseline"),
        (RunAction.FINETUNE, "Gemma"),
        (RunAction.VALIDATE, "ABTest"),
    ]

    for action, use_case in runs:
        name = builder.run_name(action=action, use_case=use_case)
        print(f"  {name}")

    # Override timestamp for reproducibility
    fixed = builder.run_name(
        action=RunAction.TRAIN,
        use_case="MiniLM",
        timestamp="20261025-1400",
    )
    print(f"  {fixed}  (fixed timestamp)")

    # ------------------------------------------------------------------
    # 4) Validate an existing name
    # ------------------------------------------------------------------
    print()
    print("=" * 60)
    print("  Name Validation")
    print("=" * 60)

    test_names = [
        "TAT_TwoTower_Baseline",  # valid
        "TAT_Ranking_Champion",  # valid
        "MyModel_v1",  # invalid (no TAT prefix)
        "TAT",  # invalid (too few segments)
    ]

    for name in test_names:
        valid = ModelNameBuilder.validate_registered_name(name)
        status = "✓ valid" if valid else "✗ invalid"
        print(f"  {name:<30} {status}")

    print()


if __name__ == "__main__":
    main()
