"""AI分类任务 UseCase 单元测试."""

from __future__ import annotations

from unittest.mock import MagicMock

import pytest

from packages.application.classification_jobs import (
    SubmitClassificationJobCommand,
    SubmitClassificationJobUseCase,
)
from packages.domain import ClassificationJob


@pytest.fixture
def mock_repo():
    return MagicMock()


class TestSubmitClassificationJobUseCase:
    """SubmitClassificationJobUseCase 测试"""

    def test_submit_job_success(self, mock_repo):
        """正常提交分类任务"""
        mock_repo.create.side_effect = lambda j: j
        use_case = SubmitClassificationJobUseCase(mock_repo)

        command = SubmitClassificationJobCommand(
            project_id="proj_001",
            asset_id="asset_001",
        )
        result = use_case.execute(command)

        assert isinstance(result, ClassificationJob)
        assert result.project_id == "proj_001"
        assert result.asset_id == "asset_001"
        assert result.status == "pending"
        assert result.confidence == 0.0
        assert result.error_message == ""
        mock_repo.create.assert_called_once()

    def test_submit_job_generates_id(self, mock_repo):
        """提交任务时生成 id"""
        mock_repo.create.side_effect = lambda j: j
        use_case = SubmitClassificationJobUseCase(mock_repo)

        command = SubmitClassificationJobCommand(
            project_id="proj_001",
            asset_id="asset_001",
        )
        result = use_case.execute(command)

        assert result.id is not None
        assert len(result.id) > 0

    def test_submit_job_two_different_ids(self, mock_repo):
        """两次提交生成不同的 id"""
        mock_repo.create.side_effect = lambda j: j
        use_case = SubmitClassificationJobUseCase(mock_repo)

        command = SubmitClassificationJobCommand(
            project_id="proj_001",
            asset_id="asset_001",
        )
        r1 = use_case.execute(command)
        r2 = use_case.execute(command)

        assert r1.id != r2.id

    def test_submit_job_initial_classification_empty(self, mock_repo):
        """初始 classification 为空"""
        mock_repo.create.side_effect = lambda j: j
        use_case = SubmitClassificationJobUseCase(mock_repo)

        command = SubmitClassificationJobCommand(
            project_id="proj_001",
            asset_id="asset_001",
        )
        result = use_case.execute(command)

        assert result.classification == ""

    def test_submit_job_returns_repo_result(self, mock_repo):
        """返回 repository.create 的结果"""
        expected = MagicMock(spec=ClassificationJob)
        mock_repo.create.return_value = expected

        use_case = SubmitClassificationJobUseCase(mock_repo)
        command = SubmitClassificationJobCommand(
            project_id="proj_001",
            asset_id="asset_001",
        )
        result = use_case.execute(command)

        assert result is expected
