"""
Pydantic schemas for custom task type management
"""
from pydantic import BaseModel, Field, validator
from typing import List, Literal
import re


class CustomTaskTypeCreate(BaseModel):
    """Schema for creating a new custom task type"""
    task_type: str = Field(..., min_length=3, max_length=50, description="Task type name")
    category: Literal["asset", "shot"] = Field(..., description="Task category (asset or shot)")
    
    @validator('task_type')
    def validate_task_type_name(cls, v):
        """Validate task type name format"""
        # Must be lowercase alphanumeric with underscores only
        if not re.match(r'^[a-z0-9_]{3,50}$', v):
            raise ValueError(
                'Task type name must be 3-50 characters, lowercase alphanumeric with underscores only'
            )
        return v


class CustomTaskTypeUpdate(BaseModel):
    """Schema for updating a custom task type name"""
    old_name: str = Field(..., description="Current task type name")
    new_name: str = Field(..., min_length=3, max_length=50, description="New task type name")
    category: Literal["asset", "shot"] = Field(..., description="Task category (asset or shot)")
    
    @validator('new_name')
    def validate_task_type_name(cls, v):
        """Validate task type name format"""
        if not re.match(r'^[a-z0-9_]{3,50}$', v):
            raise ValueError(
                'Task type name must be 3-50 characters, lowercase alphanumeric with underscores only'
            )
        return v


class CustomTaskTypeDelete(BaseModel):
    """Schema for deleting a custom task type"""
    task_type: str = Field(..., description="Task type name to delete")
    category: Literal["asset", "shot"] = Field(..., description="Task category (asset or shot)")


class AllTaskTypesResponse(BaseModel):
    """Schema for response containing all task types (standard + custom)"""
    asset_task_types: List[str] = Field(..., description="All asset task types")
    shot_task_types: List[str] = Field(..., description="All shot task types")
    standard_asset_types: List[str] = Field(..., description="Standard asset task types (read-only)")
    standard_shot_types: List[str] = Field(..., description="Standard shot task types (read-only)")
    custom_asset_types: List[str] = Field(..., description="Custom asset task types")
    custom_shot_types: List[str] = Field(..., description="Custom shot task types")


class TaskTypeInUseError(BaseModel):
    """Schema for error when trying to delete a task type in use"""
    error: str = Field(..., description="Error message")
    task_type: str = Field(..., description="Task type that is in use")
    task_count: int = Field(..., description="Number of tasks using this type")
    task_ids: List[int] = Field(..., description="IDs of tasks using this type")
