from datetime import datetime
from typing import Annotated

from bson import ObjectId
from pydantic import BaseModel, BeforeValidator


def validate_object_id(v) -> str:
    if isinstance(v, ObjectId):
        return str(v)
    if isinstance(v, str):
        return v
    raise ValueError("Invalid ObjectId")


PyObjectId = Annotated[str, BeforeValidator(validate_object_id)]


class Client(BaseModel):
    id: str | None = None
    name: str
    slug: str
    is_active: bool = True
    created_at: datetime | None = None
    updated_at: datetime | None = None


class ClientCreate(BaseModel):
    name: str
    slug: str


class ClientUpdate(BaseModel):
    name: str | None = None
    slug: str | None = None
    is_active: bool | None = None


class Team(BaseModel):
    id: str | None = None
    name: str
    client_id: str
    member_user_ids: list[str] = []
    created_at: datetime | None = None
    updated_at: datetime | None = None


class TeamCreate(BaseModel):
    name: str


class TeamUpdate(BaseModel):
    name: str | None = None


class Project(BaseModel):
    id: str | None = None
    name: str
    client_id: str
    is_active: bool = True
    default_languages: list[str] = []
    default_linguist_id: str | None = None
    default_reviewer_id: str | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None


class ProjectCreate(BaseModel):
    name: str
    default_languages: list[str] = []
    default_linguist_id: str | None = None
    default_reviewer_id: str | None = None


class ProjectUpdate(BaseModel):
    name: str | None = None
    is_active: bool | None = None
    default_languages: list[str] | None = None
    default_linguist_id: str | None = None
    default_reviewer_id: str | None = None
