from __future__ import annotations

from unittest.mock import MagicMock, patch

from supply_infra.scheduler.jobs.publish_videos_from_discovery import (
    publish_videos_from_discovery,
)
from supply_infra.services.video_discovery_service import PublishableCandidate


@patch("supply_infra.scheduler.jobs.publish_videos_from_discovery.AigcClient")
@patch(
    "supply_infra.scheduler.jobs.publish_videos_from_discovery.get_video_discovery_service"
)
@patch(
    "supply_infra.scheduler.jobs.publish_videos_from_discovery.list_unique_plan_pairs"
)
def test_publish_marks_candidates_when_bind_fails(
    mock_plan_pairs,
    mock_get_service,
    mock_client_cls,
) -> None:
    mock_plan_pairs.return_value = [
        type(
            "Plan",
            (),
            {
                "label": "plan-a",
                "produce_plan_id": "produce-1",
                "publish_plan_id": "publish-1",
            },
        )()
    ]
    mock_get_service.return_value = MagicMock(
        list_publishable_candidates=MagicMock(
            return_value=[PublishableCandidate(id=1, aweme_id="71234567890123450")]
        ),
        mark_candidates_aigc_plans=MagicMock(return_value=1),
    )
    mock_client_cls.return_value = MagicMock(
        create_video_crawler_plan=MagicMock(
            return_value={
                "success": True,
                "crawler_plan_id": "crawler-1",
                "crawler_plan_name": "test-plan",
            }
        ),
        bind_crawler_to_produce_plan=MagicMock(
            return_value={"success": False, "error": "bind failed"}
        ),
    )

    result = publish_videos_from_discovery(biz_dt="20260728")

    assert result["failed_batch_count"] == 1
    mock_get_service.return_value.mark_candidates_aigc_plans.assert_called_once()
    call_kwargs = (
        mock_get_service.return_value.mark_candidates_aigc_plans.call_args.kwargs
    )
    assert call_kwargs["crawler_plan_id"] == "crawler-1"
    assert call_kwargs["plan_label"].startswith("BIND_FAILED:")


@patch(
    "supply_infra.scheduler.jobs.publish_videos_from_discovery.get_video_discovery_service"
)
@patch(
    "supply_infra.scheduler.jobs.publish_videos_from_discovery.list_unique_plan_pairs"
)
def test_publish_any_biz_dt_skips_date_filter(
    mock_plan_pairs,
    mock_get_service,
) -> None:
    mock_plan_pairs.return_value = [
        type(
            "Plan",
            (),
            {
                "label": "plan-a",
                "produce_plan_id": "produce-1",
                "publish_plan_id": "publish-1",
            },
        )()
    ]
    mock_get_service.return_value = MagicMock(
        list_publishable_candidates=MagicMock(return_value=[]),
    )

    result = publish_videos_from_discovery(any_biz_dt=True)

    assert result["success"] is True
    assert result["any_biz_dt"] is True
    assert result["biz_dt"] is None
    mock_get_service.return_value.list_publishable_candidates.assert_called_once_with(
        run_id=None,
        biz_dt=None,
        skip_published=True,
        limit=None,
    )
