"""
Pydantic schemas for department management
"""
from pydantic import BaseModel, Field, validator
from typing import List
import re


class CustomDepartmentCreate(BaseModel):
    """Schema for creating a new custom department"""
    department: str = Field(..., min_length=2, max_length=50, description="Department name")

    @validator('department')
    def validate_department_name(cls, v):
        """Validate department name format"""
        if not re.match(r'^[a-z0-9_]{2,50}$', v):
            raise ValueError(
                'Department name must be 2-50 characters, lowercase alphanumeric with underscores only'
            )
        return v


class CustomDepartmentUpdate(BaseModel):
    """Schema for updating a custom department name"""
    old_name: str = Field(..., description="Current department name")
    new_name: str = Field(..., min_length=2, max_length=50, description="New department name")

    @validator('new_name')
    def validate_department_name(cls, v):
        """Validate department name format"""
        if not re.match(r'^[a-z0-9_]{2,50}$', v):
            raise ValueError(
                'Department name must be 2-50 characters, lowercase alphanumeric with underscores only'
            )
        return v


class AllDepartmentsResponse(BaseModel):
    """Schema for response containing all departments (standard + custom)"""
    departments: List[str] = Field(..., description="All departments")
    standard_departments: List[str] = Field(..., description="Standard departments (read-only)")
    custom_departments: List[str] = Field(..., description="Custom departments")


class DepartmentInUseError(BaseModel):
    """Schema for error when trying to delete a department in use"""
    error: str = Field(..., description="Error message")
    department: str = Field(..., description="Department that is in use")
    member_count: int = Field(..., description="Number of project members using this department")
    task_count: int = Field(..., description="Number of tasks using this department")
