from __future__ import annotations

from datetime import datetime

from pydantic import BaseModel, Field

from app.schemas.common import ORMModel


class AdminLoginRequest(BaseModel):
    username: str = Field(min_length=1, max_length=50)
    password: str = Field(min_length=1, max_length=100)


class AdminLoginResponse(BaseModel):
    token: str
    expiredAt: datetime
    admin: dict


class AdminRead(ORMModel):
    id: int
    username: str
    name: str
    status: int


class DashboardStats(BaseModel):
    userCount: int
    sessionCount: int
    messageCount: int
    aiRequestCount: int
    knowledgeCount: int


class AdminUserUpdateRequest(BaseModel):
    status: int | None = Field(default=None, ge=0, le=1)
    dailyChatLimit: int | None = Field(default=None, ge=0, le=100000)
    expiredAt: datetime | None = None


class AdminUserCreateRequest(BaseModel):
    phone: str = Field(min_length=11, max_length=20)
    name: str = Field(min_length=1, max_length=50)
    nickname: str | None = Field(default=None, max_length=50)
    status: int = Field(default=1, ge=0, le=1)
    dailyChatLimit: int | None = Field(default=None, ge=0, le=100000)
    expiredAt: datetime | None = None


class AdminUserImportItem(BaseModel):
    phone: str = Field(default="", max_length=20)
    name: str = Field(default="", max_length=50)
    nickname: str | None = Field(default=None, max_length=50)
    status: int = Field(default=1, ge=0, le=1)
    dailyChatLimit: int | None = Field(default=None, ge=0, le=100000)
    expiredAt: datetime | None = None


class AdminUserImportRequest(BaseModel):
    students: list[AdminUserImportItem] = Field(default_factory=list)


class KnowledgeSaveRequest(BaseModel):
    name: str = Field(min_length=1, max_length=100)
    feishuSpaceId: str = Field(min_length=1, max_length=100)
    feishuNodeId: str = Field(min_length=1, max_length=100)
    status: int = Field(default=1, ge=0, le=1)
    remark: str | None = Field(default=None, max_length=255)


class PromptSaveRequest(BaseModel):
    promptContent: str = Field(min_length=1)


class AgentDebugRequest(BaseModel):
    promptContent: str = Field(min_length=1)
    modelId: int = Field(gt=0)
    knowledgeIds: list[int] = Field(default_factory=list)
    question: str = Field(min_length=1, max_length=2000)
    temperature: float | None = Field(default=None, ge=0, le=2)
    topP: float | None = Field(default=None, ge=0, le=1)
    topK: int | None = Field(default=None, ge=1, le=1000)
    presencePenalty: float | None = Field(default=None, ge=-2, le=2)
    frequencyPenalty: float | None = Field(default=None, ge=-2, le=2)
    maxToken: int | None = Field(default=1024, ge=1, le=100000)


class ModelSaveRequest(BaseModel):
    provider: str = Field(min_length=1, max_length=50)
    displayName: str | None = Field(default=None, max_length=100)
    apiType: str = Field(default="openai_compatible", max_length=50)
    modelName: str = Field(min_length=1, max_length=100)
    baseUrl: str | None = Field(default=None, max_length=255)
    apiUrl: str = Field(default="", max_length=255)
    apiKey: str = ""
    authType: str = Field(default="bearer", max_length=30)
    apiVersion: str | None = Field(default=None, max_length=50)
    temperature: float | None = Field(default=0.2, ge=0, le=2)
    topP: float | None = Field(default=None, ge=0, le=1)
    topK: int | None = Field(default=None, ge=1, le=1000)
    presencePenalty: float | None = Field(default=None, ge=-2, le=2)
    frequencyPenalty: float | None = Field(default=None, ge=-2, le=2)
    maxToken: int | None = Field(default=1024, ge=1, le=100000)
    contextWindow: int | None = Field(default=None, ge=1, le=2000000)
    streamEnabled: int = Field(default=1, ge=0, le=1)
    responseFormat: str | None = Field(default=None, max_length=50)
    extraParams: str | None = None
    remark: str | None = Field(default=None, max_length=255)
    timeoutSecond: int = Field(default=30, ge=1, le=300)


class EnableModelRequest(BaseModel):
    modelId: int


class SystemConfigSaveRequest(BaseModel):
    configKey: str = Field(min_length=1, max_length=100)
    configValue: str
    description: str | None = Field(default=None, max_length=255)
