from __future__ import annotations

from datetime import datetime
from decimal import Decimal

from sqlalchemy import BigInteger, DateTime, Integer, Numeric, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base


class Prompt(Base):
    __tablename__ = "sys_prompt"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    prompt_content: Mapped[str] = mapped_column(Text, nullable=False)
    updated_by: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now(), nullable=False)


class ModelConfig(Base):
    __tablename__ = "sys_model"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    provider: Mapped[str] = mapped_column(String(50), nullable=False)
    display_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    api_type: Mapped[str] = mapped_column(String(50), default="openai_compatible", nullable=False)
    model_name: Mapped[str] = mapped_column(String(100), nullable=False)
    base_url: Mapped[str | None] = mapped_column(String(255), nullable=True)
    api_url: Mapped[str] = mapped_column(String(255), nullable=False)
    api_key: Mapped[str] = mapped_column(Text, nullable=False)
    auth_type: Mapped[str] = mapped_column(String(30), default="bearer", nullable=False)
    api_version: Mapped[str | None] = mapped_column(String(50), nullable=True)
    temperature: Mapped[Decimal | None] = mapped_column(Numeric(4, 2), nullable=True)
    top_p: Mapped[Decimal | None] = mapped_column(Numeric(4, 2), nullable=True)
    top_k: Mapped[int | None] = mapped_column(Integer, nullable=True)
    presence_penalty: Mapped[Decimal | None] = mapped_column(Numeric(4, 2), nullable=True)
    frequency_penalty: Mapped[Decimal | None] = mapped_column(Numeric(4, 2), nullable=True)
    max_token: Mapped[int | None] = mapped_column(Integer, nullable=True)
    context_window: Mapped[int | None] = mapped_column(Integer, nullable=True)
    stream_enabled: Mapped[int] = mapped_column(default=1, nullable=False)
    response_format: Mapped[str | None] = mapped_column(String(50), nullable=True)
    extra_params: Mapped[str | None] = mapped_column(Text, nullable=True)
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    timeout_second: Mapped[int] = mapped_column(Integer, default=30, nullable=False)
    enabled: Mapped[int] = mapped_column(default=0, nullable=False)


class SystemConfig(Base):
    __tablename__ = "sys_system_config"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    config_key: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
    config_value: Mapped[str] = mapped_column(Text, nullable=False)
    description: Mapped[str | None] = mapped_column(String(255), nullable=True)
    updated_by: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now(), nullable=False)
