from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field


class RemnawaveBaseModel(BaseModel):
    model_config = ConfigDict(populate_by_name=True, extra="ignore")


class ActiveInternalSquad(RemnawaveBaseModel):
    uuid: UUID
    name: str


class UserTraffic(RemnawaveBaseModel):
    used_traffic_bytes: float = Field(alias="usedTrafficBytes")
    lifetime_used_traffic_bytes: float = Field(alias="lifetimeUsedTrafficBytes")
    online_at: datetime | None = Field(default=None, alias="onlineAt")
    first_connected_at: datetime | None = Field(default=None, alias="firstConnectedAt")
    last_connected_node_uuid: UUID | None = Field(default=None, alias="lastConnectedNodeUuid")


class RemnawaveUser(RemnawaveBaseModel):
    uuid: UUID
    id: int
    short_uuid: str = Field(alias="shortUuid")
    username: str
    status: str
    traffic_limit_bytes: int = Field(alias="trafficLimitBytes")
    expire_at: datetime = Field(alias="expireAt")
    telegram_id: int | None = Field(default=None, alias="telegramId")
    email: str | None = None
    description: str | None = None
    tag: str | None = None
    hwid_device_limit: int | None = Field(default=None, alias="hwidDeviceLimit")
    external_squad_uuid: UUID | None = Field(default=None, alias="externalSquadUuid")
    trojan_password: str = Field(alias="trojanPassword")
    vless_uuid: UUID = Field(alias="vlessUuid")
    ss_password: str = Field(alias="ssPassword")
    last_triggered_threshold: int = Field(alias="lastTriggeredThreshold")
    sub_revoked_at: datetime | None = Field(default=None, alias="subRevokedAt")
    sub_last_user_agent: str | None = Field(default=None, alias="subLastUserAgent")
    sub_last_opened_at: datetime | None = Field(default=None, alias="subLastOpenedAt")
    last_traffic_reset_at: datetime | None = Field(default=None, alias="lastTrafficResetAt")
    created_at: datetime = Field(alias="createdAt")
    updated_at: datetime = Field(alias="updatedAt")
    subscription_url: str = Field(alias="subscriptionUrl")
    active_internal_squads: list[ActiveInternalSquad] = Field(alias="activeInternalSquads")
    user_traffic: UserTraffic = Field(alias="userTraffic")


class PaginatedUsers(RemnawaveBaseModel):
    users: list[RemnawaveUser]
    total: int


class ResolvedUser(RemnawaveBaseModel):
    uuid: UUID
    username: str
    id: int
    short_uuid: str = Field(alias="shortUuid")


class SubscriptionRequestRecord(RemnawaveBaseModel):
    id: int
    user_uuid: UUID = Field(alias="userUuid")
    request_at: datetime = Field(alias="requestAt")
    request_ip: str | None = Field(default=None, alias="requestIp")
    user_agent: str | None = Field(default=None, alias="userAgent")


class SubscriptionRequestHistory(RemnawaveBaseModel):
    total: int
    records: list[SubscriptionRequestRecord]


class HwidDevice(RemnawaveBaseModel):
    hwid: str
    user_uuid: UUID = Field(alias="userUuid")
    platform: str | None = None
    os_version: str | None = Field(default=None, alias="osVersion")
    device_model: str | None = Field(default=None, alias="deviceModel")
    user_agent: str | None = Field(default=None, alias="userAgent")
    created_at: datetime = Field(alias="createdAt")
    updated_at: datetime = Field(alias="updatedAt")


class HwidDevicesResponse(RemnawaveBaseModel):
    total: int
    devices: list[HwidDevice]
