"""
Test custom task status schemas
"""
import pytest
from pydantic import ValidationError
from schemas.custom_task_status import (
    CustomTaskStatus,
    CustomTaskStatusCreate,
    CustomTaskStatusUpdate,
    CustomTaskStatusReorder,
    CustomTaskStatusDelete,
    SystemTaskStatus,
    AllTaskStatusesResponse,
    TaskStatusInUseError,
    CustomTaskStatusResponse
)


def test_custom_task_status_create_valid():
    """Test creating a valid custom task status"""
    status = CustomTaskStatusCreate(
        name="In Review",
        color="#FF5733"
    )
    assert status.name == "In Review"
    assert status.color == "#FF5733"


def test_custom_task_status_create_without_color():
    """Test creating a status without color (should be optional)"""
    status = CustomTaskStatusCreate(name="Pending Approval")
    assert status.name == "Pending Approval"
    assert status.color is None


def test_custom_task_status_create_name_validation():
    """Test name validation - empty name should fail"""
    with pytest.raises(ValidationError) as exc_info:
        CustomTaskStatusCreate(name="   ")
    assert "Status name cannot be empty" in str(exc_info.value)


def test_custom_task_status_create_name_too_long():
    """Test name validation - name too long should fail"""
    long_name = "A" * 51
    with pytest.raises(ValidationError) as exc_info:
        CustomTaskStatusCreate(name=long_name)
    # Pydantic's built-in validation message
    assert "at most 50 characters" in str(exc_info.value)


def test_custom_task_status_create_invalid_color():
    """Test color validation - invalid hex code should fail"""
    with pytest.raises(ValidationError) as exc_info:
        CustomTaskStatusCreate(name="Test", color="invalid")
    assert "Color must be a valid hex code" in str(exc_info.value)


def test_custom_task_status_create_color_normalization():
    """Test color normalization to uppercase"""
    status = CustomTaskStatusCreate(name="Test", color="#ff5733")
    assert status.color == "#FF5733"


def test_custom_task_status_update_valid():
    """Test updating a custom task status"""
    update = CustomTaskStatusUpdate(
        name="Updated Name",
        color="#00FF00",
        is_default=True
    )
    assert update.name == "Updated Name"
    assert update.color == "#00FF00"
    assert update.is_default is True


def test_custom_task_status_update_partial():
    """Test partial update (only some fields)"""
    update = CustomTaskStatusUpdate(name="New Name")
    assert update.name == "New Name"
    assert update.color is None
    assert update.is_default is None


def test_custom_task_status_reorder_valid():
    """Test reordering statuses"""
    reorder = CustomTaskStatusReorder(
        status_ids=["status_1", "status_2", "status_3"]
    )
    assert len(reorder.status_ids) == 3


def test_custom_task_status_reorder_empty():
    """Test reordering with empty list should fail"""
    with pytest.raises(ValidationError):
        CustomTaskStatusReorder(status_ids=[])


def test_custom_task_status_reorder_duplicates():
    """Test reordering with duplicates should fail"""
    with pytest.raises(ValidationError) as exc_info:
        CustomTaskStatusReorder(status_ids=["status_1", "status_1", "status_2"])
    assert "duplicates" in str(exc_info.value).lower()


def test_custom_task_status_delete():
    """Test delete schema"""
    delete = CustomTaskStatusDelete(reassign_to_status_id="status_2")
    assert delete.reassign_to_status_id == "status_2"


def test_custom_task_status_delete_without_reassignment():
    """Test delete without reassignment"""
    delete = CustomTaskStatusDelete()
    assert delete.reassign_to_status_id is None


def test_custom_task_status_full():
    """Test full custom task status schema"""
    status = CustomTaskStatus(
        id="custom_status_1",
        name="In Review",
        color="#FF5733",
        order=0,
        is_default=False
    )
    assert status.id == "custom_status_1"
    assert status.name == "In Review"
    assert status.color == "#FF5733"
    assert status.order == 0
    assert status.is_default is False


def test_system_task_status():
    """Test system task status schema"""
    status = SystemTaskStatus(
        id="not_started",
        name="Not Started",
        color="#6B7280"
    )
    assert status.id == "not_started"
    assert status.is_system is True


def test_all_task_statuses_response():
    """Test all task statuses response schema"""
    response = AllTaskStatusesResponse(
        statuses=[
            CustomTaskStatus(
                id="custom_1",
                name="Custom Status",
                color="#FF5733",
                order=0,
                is_default=False
            )
        ],
        system_statuses=[
            SystemTaskStatus(
                id="not_started",
                name="Not Started",
                color="#6B7280"
            )
        ],
        default_status_id="not_started"
    )
    assert len(response.statuses) == 1
    assert len(response.system_statuses) == 1
    assert response.default_status_id == "not_started"


def test_task_status_in_use_error():
    """Test task status in use error schema"""
    error = TaskStatusInUseError(
        error="Cannot delete status in use",
        status_id="custom_1",
        status_name="In Review",
        task_count=5,
        task_ids=[1, 2, 3, 4, 5]
    )
    assert error.task_count == 5
    assert len(error.task_ids) == 5


def test_custom_task_status_response():
    """Test custom task status response schema"""
    response = CustomTaskStatusResponse(
        message="Status created successfully",
        status=CustomTaskStatus(
            id="custom_1",
            name="New Status",
            color="#FF5733",
            order=0,
            is_default=False
        )
    )
    assert response.message == "Status created successfully"
    assert response.status.name == "New Status"


if __name__ == "__main__":
    pytest.main([__file__, "-v"])
