from autoshorts.content.brief import create_content_brief
from autoshorts.content.script_draft import create_script_draft
from autoshorts.content.to_candidate import convert_script_draft_to_candidate
from autoshorts.rendering.manifest import build_render_manifest
from autoshorts.rendering.preview import build_html_preview
from autoshorts.rendering.shot_plan import create_shot_plan
from autoshorts.social.draft import build_social_media_draft
from autoshorts.social.manual_posting import (
    ManualPlatformPost,
    build_manual_posting_pack,
    render_manual_posting_instructions,
    validate_manual_posting_pack,
)


def _draft():
    brief = create_content_brief(
        pillar_id="ai_life_systems",
        topic="AI weekly review routine",
        audience_problem="I use AI tools but still lose control of priorities.",
        payoff="a 10-minute Sunday review that keeps AI as assistant, not boss",
        platforms=("youtube", "tiktok"),
    )
    script = create_script_draft(brief)
    candidate = convert_script_draft_to_candidate(brief, script)
    preview = build_html_preview(build_render_manifest(create_shot_plan(script)))
    return build_social_media_draft(candidate, preview)


def test_build_manual_posting_pack_creates_youtube_and_tiktok_copy_without_upload_side_effects():
    draft = _draft()

    pack = build_manual_posting_pack(draft, video_path="/tmp/final-video.mp4")

    assert pack.candidate_id == "ai_life_systems-ai-weekly-review-routine-35s"
    assert pack.phase == "manual_posting"
    assert pack.video_path == "/tmp/final-video.mp4"
    assert pack.requires_human_manual_upload is True
    assert pack.side_effects == ()
    assert pack.external_calls == ()
    assert tuple(post.platform for post in pack.platform_posts) == ("youtube", "tiktok")

    youtube = pack.platform_posts[0]
    assert isinstance(youtube, ManualPlatformPost)
    assert youtube.platform == "youtube"
    assert youtube.title == "AI weekly review routine"
    assert "Manual upload: copy this description into YouTube Shorts." in youtube.instructions
    assert draft.caption in youtube.description
    assert draft.cta in youtube.description
    assert "#AI" in youtube.description
    assert youtube.tags == ("AI", "LifeSystems", "Productivity")

    tiktok = pack.platform_posts[1]
    assert tiktok.platform == "tiktok"
    assert tiktok.title == "AI weekly review routine"
    assert "Manual upload: copy this caption into TikTok." in tiktok.instructions
    assert tiktok.tags == ()
    assert len(tiktok.description) <= 2200


def test_validate_manual_posting_pack_rejects_platform_actions_and_missing_manual_upload():
    draft = _draft()
    pack = build_manual_posting_pack(draft, video_path="/tmp/final-video.mp4")
    unsafe = type(pack)(
        candidate_id=pack.candidate_id,
        phase=pack.phase,
        video_path=pack.video_path,
        platform_posts=pack.platform_posts,
        requires_human_manual_upload=False,
        side_effects=("upload_video",),
        external_calls=("youtube_api",),
    )

    result = validate_manual_posting_pack(unsafe)

    assert result.is_valid is False
    assert "human manual upload is required in phase 1" in result.errors
    assert "manual posting packs must not have side effects" in result.errors
    assert "manual posting packs must not call external services" in result.errors


def test_render_manual_posting_instructions_contains_video_path_and_platform_copy():
    pack = build_manual_posting_pack(_draft(), video_path="/tmp/final-video.mp4")

    text = render_manual_posting_instructions(pack)

    assert "## Manual Posting Pack" in text
    assert "Video file: /tmp/final-video.mp4" in text
    assert "## YouTube Shorts" in text
    assert "## TikTok" in text
    assert "Title:" in text
    assert "Description:" in text
    assert "Tags:" in text
    assert "No platform upload was performed." in text
