"""Print the first TrueTraceShorts format-family test wave.

Side-effect-free: no rendering, uploads, Telegram sends, or file writes.
"""

from __future__ import annotations

import argparse
import sys
from typing import Sequence, TextIO

from autoshorts.strategy.format_family import build_first_test_wave, format_mix_summary, render_test_wave_json, validate_format_candidate_plan


def main(argv: Sequence[str] | None = None, *, stdout: TextIO | None = None) -> int:
    parser = argparse.ArgumentParser(description="Show the planned TrueTraceShorts format-family test wave.")
    parser.add_argument("--json", action="store_true", help="Print machine-readable candidate plans.")
    args = parser.parse_args(argv)
    out = stdout or sys.stdout
    wave = build_first_test_wave()
    errors: list[str] = []
    for plan in wave:
        result = validate_format_candidate_plan(plan)
        if not result.is_valid:
            errors.extend(f"{plan.candidate_id}: {err}" for err in result.errors)
    if errors:
        for err in errors:
            print(err, file=out)
        return 3
    if args.json:
        print(render_test_wave_json(wave), end="", file=out)
        return 0
    print("TrueTraceShorts format-family mix for next 20 videos:", file=out)
    for key, value in format_mix_summary().items():
        print(f"- {key}: {value}", file=out)
    print("", file=out)
    print("First 5-video test wave:", file=out)
    for idx, plan in enumerate(wave, 1):
        meta = plan.format_metadata
        print(f"{idx}. {plan.title}", file=out)
        print(f"   candidate_id: {plan.candidate_id}", file=out)
        print(f"   format_type: {meta.format_type.value}", file=out)
        print(f"   hook_type: {meta.hook_type.value}", file=out)
        print(f"   viewer_state: {meta.viewer_state.value}", file=out)
        print(f"   website_target: {meta.website_target}", file=out)
        print(f"   first_frame_scam_label: {meta.first_frame_scam_label}", file=out)
        print(f"   cta: {plan.cta}", file=out)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
