import uuid from pydantic import BaseModel, ConfigDict, EmailStr, Field, model_validator from app.common.enums import Role, FIELD_ROLES class UserCreate(BaseModel): email: EmailStr password: str = Field(min_length=8, max_length=128) full_name: str = Field(min_length=2, max_length=200) employee_code: str = Field(min_length=1, max_length=50) role: Role phone: str | None = Field(default=None, max_length=20) #: Required for MR, forbidden for every other role (R4/R8). headquarters_id: uuid.UUID | None = None #: SFE only: the other districts they cover besides `headquarters_id`. extra_headquarter_ids: list[uuid.UUID] = [] @model_validator(mode="after") def check_hq_matches_role(self): if self.extra_headquarter_ids and self.role != Role.SFE: raise ValueError("extra_headquarter_ids may only be set for an SFE") if self.role in FIELD_ROLES and self.headquarters_id is None: raise ValueError("headquarters_id is required for an MR") if self.role not in FIELD_ROLES and self.headquarters_id is not None: raise ValueError("headquarters_id may only be set for an MR") return self class UserUpdate(BaseModel): full_name: str | None = Field(default=None, min_length=2, max_length=200) #: Sign-in credentials. Admin-only (the whole endpoint is); changing them is audited. email: EmailStr | None = None employee_code: str | None = Field(default=None, min_length=1, max_length=50) #: A new password. Signs the user out everywhere and clears a lockout. password: str | None = Field(default=None, min_length=8, max_length=128) phone: str | None = Field(default=None, max_length=20) #: Admin-only. Moving to MR needs `headquarters_id`; leaving MR or ABM ends that role's #: assignments, and the user is signed out so they sign in again with the new role. role: Role | None = None headquarters_id: uuid.UUID | None = None #: SFE only. The full list of extra districts; omit to leave it unchanged. extra_headquarter_ids: list[uuid.UUID] | None = None is_active: bool | None = None class UserRead(BaseModel): model_config = ConfigDict(from_attributes=True) id: uuid.UUID email: str full_name: str employee_code: str role: Role phone: str | None headquarters_id: uuid.UUID | None is_active: bool is_superadmin: bool = False extra_headquarter_ids: list[uuid.UUID] = [] class MyProfile(BaseModel): """S4 — the MR profile screen. Includes the DERIVED reporting Area Business Manager.""" id: uuid.UUID full_name: str employee_code: str email: str role: Role phone: str | None headquarters_id: uuid.UUID | None headquarters_code: str | None headquarters_name: str | None reporting_manager_id: uuid.UUID | None reporting_manager_name: str | None