# [DEF:backend.src.models.llm:Module]
# @TIER: STANDARD
# @SEMANTICS: llm, models, sqlalchemy, persistence
# @PURPOSE: SQLAlchemy models for LLM provider configuration and validation results.
# @LAYER: Domain
# @RELATION: INHERITS_FROM -> backend.src.models.mapping.Base

from sqlalchemy import Column, String, Boolean, DateTime, JSON, Text, Time, ForeignKey
from datetime import datetime
import uuid
from .mapping import Base

def generate_uuid():
    return str(uuid.uuid4())

# [DEF:ValidationPolicy:Class]
# @PURPOSE: Defines a scheduled rule for validating a group of dashboards within an execution window.
class ValidationPolicy(Base):
    __tablename__ = "validation_policies"

    id = Column(String, primary_key=True, default=generate_uuid)
    name = Column(String, nullable=False)
    environment_id = Column(String, nullable=False)
    is_active = Column(Boolean, default=True)
    dashboard_ids = Column(JSON, nullable=False) # Array of dashboard IDs
    schedule_days = Column(JSON, nullable=False) # Array of integers (0-6)
    window_start = Column(Time, nullable=False)
    window_end = Column(Time, nullable=False)
    notify_owners = Column(Boolean, default=True)
    custom_channels = Column(JSON, nullable=True) # List of external channels
    alert_condition = Column(String, default="FAIL_ONLY") # FAIL_ONLY, WARN_AND_FAIL, ALWAYS
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# [/DEF:ValidationPolicy:Class]

# [DEF:LLMProvider:Class]
# @PURPOSE: SQLAlchemy model for LLM provider configuration.
class LLMProvider(Base):
    __tablename__ = "llm_providers"
    
    id = Column(String, primary_key=True, default=generate_uuid)
    provider_type = Column(String, nullable=False) # openai, openrouter, kilo
    name = Column(String, nullable=False)
    base_url = Column(String, nullable=False)
    api_key = Column(String, nullable=False) # Should be encrypted
    default_model = Column(String, nullable=False)
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=datetime.utcnow)
# [/DEF:LLMProvider:Class]

# [DEF:ValidationRecord:Class]
# @PURPOSE: SQLAlchemy model for dashboard validation history.
class ValidationRecord(Base):
    __tablename__ = "llm_validation_results"
    
    id = Column(String, primary_key=True, default=generate_uuid)
    task_id = Column(String, nullable=True, index=True) # Reference to TaskRecord
    dashboard_id = Column(String, nullable=False, index=True)
    environment_id = Column(String, nullable=True, index=True)
    timestamp = Column(DateTime, default=datetime.utcnow)
    status = Column(String, nullable=False) # PASS, WARN, FAIL, UNKNOWN
    screenshot_path = Column(String, nullable=True)
    issues = Column(JSON, nullable=False)
    summary = Column(Text, nullable=False)
    raw_response = Column(Text, nullable=True)
# [/DEF:ValidationRecord:Class]

# [/DEF:backend.src.models.llm:Module]