"""
Database table schemas documented as attrs classes.

These are NOT ORM models — they exist purely for documentation and
type-safe row construction. The actual queries are raw SQL in _queries.py.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

import attrs

if TYPE_CHECKING:
    from datetime import datetime


@attrs.frozen
class CFAPIKeysRow:
    """
    Row from the ``cf_api_keys`` table.
    """

    id: int
    key: str
    suffix: str
    name: str
    created_at: datetime
    last_used: datetime | None = None
    user_id: str | None = None
    tier: str | None = None
    organization_id: str | None = None


@attrs.frozen
class UsersRow:
    """
    Row from the ``users`` table.
    """

    user_id: str
    github_username: str
    email: str | None = None
    name: str | None = None
    onboarding_completed: bool = False
    created_at: datetime | None = None


@attrs.frozen
class OrganizationsRow:
    """
    Row from the ``organizations`` table.
    """

    id: str
    name: str
    github_org_id: str | None = None
    description: str | None = None
    website: str | None = None
    auto_add_github_members: bool = True
    added_by: str | None = None
    privacy_mode: bool = False
    subscription: bool = False
    created_at: datetime | None = None
    updated_at: datetime | None = None


@attrs.frozen
class SubscriptionsRow:
    """
    Row from the ``subscriptions`` table.
    """

    id: str
    user_id: str
    plan_type: str
    optimizations_used: int = 0
    total_lifetime_optimizations: int = 0
    optimizations_limit: int = 4000
    subscription_status: str = "active"
    stripe_customer_id: str | None = None
    stripe_subscription_id: str | None = None
    current_period_start: datetime | None = None
    current_period_end: datetime | None = None
    cancel_at_period_end: bool = False
    cancellation_request_date: datetime | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None


@attrs.frozen
class LLMCallsRow:
    """
    Row from the ``llm_calls`` table.
    """

    trace_id: str
    call_type: str
    model_name: str
    input_tokens: int
    output_tokens: int
    cached_tokens: int = 0
    cost: float = 0.0
    latency_ms: int = 0
    created_at: datetime | None = None


@attrs.frozen
class OptimizationErrorsRow:
    """
    Row from the ``optimization_errors`` table.
    """

    trace_id: str
    error_type: str
    severity: str
    stack_trace: str | None = None
    created_at: datetime | None = None


@attrs.frozen
class OptimizationFeaturesRow:
    """
    Row from the ``optimization_features`` table.
    """

    trace_id: str
    user_id: str
    original_code: str
    speedup_ratio: float | None = None
    created_at: datetime | None = None
