from datetime import datetime

from pydantic import BaseModel, ConfigDict, Field


class ClientUserCreate(BaseModel):
    model_config = ConfigDict(extra="forbid")
    client_account_id: int
    full_name: str = Field(min_length=1, max_length=255)
    email: str = Field(min_length=3, max_length=255)
    role: str = "viewer"
    status: str = "invited"
    is_new_user: bool = True


class ClientUserUpdate(BaseModel):
    model_config = ConfigDict(extra="forbid")
    full_name: str | None = Field(default=None, min_length=1, max_length=255)
    email: str | None = Field(default=None, min_length=3, max_length=255)
    role: str | None = None
    status: str | None = None
    is_new_user: bool | None = None


class ClientFeatureUpdate(BaseModel):
    model_config = ConfigDict(extra="forbid")
    enabled: bool


class ClientUserModulePermissionUpdate(BaseModel):
    model_config = ConfigDict(extra="forbid")
    access_level: str


class ClientUserRead(BaseModel):
    id: int
    client_account_id: int
    full_name: str
    email: str
    role: str
    status: str
    is_new_user: bool
    last_login_at: datetime | None
    created_at: datetime
    module_permissions: list["ClientUserModulePermissionRead"]


class ClientFeatureRead(BaseModel):
    id: int
    client_account_id: int
    feature_key: str
    feature_name: str
    feature_group: str
    description: str | None
    enabled: bool
    updated_at: datetime
    created_at: datetime


class ClientUserModulePermissionRead(BaseModel):
    id: int
    client_account_id: int
    client_user_id: int
    module_key: str
    module_name: str
    module_group: str
    description: str | None
    access_level: str
    updated_at: datetime
    created_at: datetime


class ClientAccessAuditEventRead(BaseModel):
    id: int
    client_account_id: int
    actor_type: str
    actor_name: str
    actor_email: str
    actor_role: str
    action: str
    target_type: str
    target_id: int | None
    module_key: str | None
    summary: str
    created_at: datetime


class ClientAccessRead(BaseModel):
    id: int
    tenant_id: str
    name: str
    client_code: str
    status: str
    powerbi_workspace: str | None
    notes: str | None
    created_at: datetime
    users: list[ClientUserRead]
    features: list[ClientFeatureRead]
    active_user_count: int
    new_user_count: int
    enabled_feature_count: int
    total_feature_count: int
    audit_history: list[ClientAccessAuditEventRead]


ClientUserRead.model_rebuild()
