"""
SQLModel Database Models

Defines the database schema using SQLModel (SQLAlchemy + Pydantic).
"""

from datetime import datetime
from typing import Any
from uuid import UUID, uuid4

from sqlmodel import Field, SQLModel, Column, JSON


class ApiKey(SQLModel, table=True):
    """API key configuration and limits."""

    __tablename__ = "api_keys"

    api_key: str = Field(primary_key=True, max_length=255)
    name: str = Field(max_length=255)
    is_active: bool = Field(default=True)
    requests_per_minute: int = Field(default=10)
    max_concurrent_jobs: int = Field(default=3)
    max_file_size_mb: int = Field(default=50)
    total_requests: int = Field(default=0)
    total_processed: int = Field(default=0)
    created_at: datetime = Field(default_factory=datetime.utcnow)
    last_used_at: datetime | None = Field(default=None)


class AsyncRequest(SQLModel, table=True):
    """Async request record."""

    __tablename__ = "async_requests"

    request_id: UUID = Field(default_factory=uuid4, primary_key=True)
    api_key: str = Field(foreign_key="api_keys.api_key", max_length=255, index=True)
    status: str = Field(default="pending", max_length=20, index=True)
    filename: str = Field(max_length=255)
    file_size: int
    content_type: str = Field(max_length=100)
    document_id: str | None = Field(default=None, max_length=100)
    error_message: str | None = Field(default=None)
    retry_count: int = Field(default=0)
    created_at: datetime = Field(default_factory=datetime.utcnow)
    started_at: datetime | None = Field(default=None)
    completed_at: datetime | None = Field(default=None)
    expires_at: datetime = Field(index=True)
    result: dict[str, Any] | None = Field(default=None, sa_column=Column(JSON))
    processing_time_ms: float | None = Field(default=None)
    visualization_path: str | None = Field(default=None, max_length=255)


class RateLimitEvent(SQLModel, table=True):
    """Rate limit event record."""

    __tablename__ = "rate_limit_events"

    id: int | None = Field(default=None, primary_key=True)
    api_key: str = Field(foreign_key="api_keys.api_key", max_length=255, index=True)
    event_type: str = Field(max_length=50)
    created_at: datetime = Field(default_factory=datetime.utcnow, index=True)


# Read-only models for responses (without table=True)
class ApiKeyRead(SQLModel):
    """API key response model (read-only)."""

    api_key: str
    name: str
    is_active: bool
    requests_per_minute: int
    max_concurrent_jobs: int
    max_file_size_mb: int


class AsyncRequestRead(SQLModel):
    """Async request response model (read-only)."""

    request_id: UUID
    api_key: str
    status: str
    filename: str
    file_size: int
    content_type: str
    document_id: str | None
    error_message: str | None
    retry_count: int
    created_at: datetime
    started_at: datetime | None
    completed_at: datetime | None
    expires_at: datetime
    result: dict[str, Any] | None
    processing_time_ms: float | None
    visualization_path: str | None
