from typing import Optional, List, Annotated
from pydantic import BaseModel, Field, ConfigDict, BeforeValidator
from datetime import datetime

StrId = Annotated[str, BeforeValidator(str)]


# Department Schemas
class DepartmentBase(BaseModel):
    name: str = Field(..., max_length=255)
    description: Optional[str] = None


class DepartmentCreate(DepartmentBase):
    pass


class DepartmentUpdate(BaseModel):
    name: Optional[str] = Field(None, max_length=255)
    description: Optional[str] = None


class DepartmentResponse(DepartmentBase):
    id: StrId
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


# Team Schemas
class TeamBase(BaseModel):
    name: str = Field(..., max_length=255)
    description: Optional[str] = None
    department_id: str = Field(..., max_length=36)


class TeamCreate(TeamBase):
    pass


class TeamUpdate(BaseModel):
    name: Optional[str] = Field(None, max_length=255)
    description: Optional[str] = None
    department_id: Optional[str] = Field(None, max_length=36)


class TeamResponse(TeamBase):
    id: StrId
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


# Userteam Schemas
class UserteamBase(BaseModel):
    user_id: str = Field(..., max_length=36)
    team_id: str = Field(..., max_length=36)


class UserteamCreate(UserteamBase):
    pass


class UserteamUpdate(BaseModel):
    user_id: Optional[str] = Field(None, max_length=36)
    team_id: Optional[str] = Field(None, max_length=36)


class UserteamResponse(UserteamBase):
    id: StrId
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


# Paginated Response
class PaginatedDepartmentResponse(BaseModel):
    items: List[DepartmentResponse]
    total: int
    limit: int
    offset: int

    model_config = ConfigDict(from_attributes=True)


class PaginatedTeamResponse(BaseModel):
    items: List[TeamResponse]
    total: int
    limit: int
    offset: int

    model_config = ConfigDict(from_attributes=True)


class PaginatedUserteamResponse(BaseModel):
    items: List[UserteamResponse]
    total: int
    limit: int
    offset: int

    model_config = ConfigDict(from_attributes=True)


# Detail Schemas for Team
class UserSummary(BaseModel):
    id: StrId
    first_name: str
    last_name: str
    email: str

    model_config = ConfigDict(from_attributes=True)


class UserteamDetail(BaseModel):
    id: StrId
    user: UserSummary
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)


class DepartmentSummary(BaseModel):
    id: StrId
    name: str
    description: Optional[str] = None

    model_config = ConfigDict(from_attributes=True)


class TeamDetailResponse(BaseModel):
    id: StrId
    name: str
    description: Optional[str] = None
    department_id: StrId
    department: DepartmentSummary
    user_teams: List[UserteamDetail]
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)