# Copy the content from the database_models_file artifact above
# src/models/database_models.py
"""
SQLAlchemy database models for the self-improving code generator
"""

from sqlalchemy import Column, String, Float, JSON, DateTime, Integer, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import func
from datetime import datetime

Base = declarative_base()

class ProjectAnalysisModel(Base):
    """Store project analysis results"""
    __tablename__ = "project_analyses"
    
    id = Column(String, primary_key=True)
    project_id = Column(String, nullable=False, index=True)
    technology_stack = Column(String, nullable=False)
    confidence_score = Column(Float, nullable=False)
    overall_score = Column(Float, nullable=False)
    analysis_data = Column(JSON, nullable=False)
    missing_components = Column(JSON, nullable=True)
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())

class ImprovementHistoryModel(Base):
    """Track applied improvements"""
    __tablename__ = "improvement_history"
    
    id = Column(String, primary_key=True)
    project_id = Column(String, nullable=False, index=True)
    user_id = Column(String, nullable=True, index=True)
    improvement_id = Column(String, nullable=False)
    improvement_type = Column(String, nullable=False)
    level = Column(String, nullable=False)  # CRITICAL, CORE, ADVANCED
    category = Column(String, nullable=False)
    action = Column(String, nullable=False)  # applied, rejected, deferred
    improvement_data = Column(JSON, nullable=False)
    applied_at = Column(DateTime, default=func.now())

class UserPreferencesModel(Base):
    """Store user learning preferences"""
    __tablename__ = "user_preferences"
    
    user_id = Column(String, primary_key=True)
    accepted_patterns = Column(JSON, nullable=True, default=list)
    rejected_patterns = Column(JSON, nullable=True, default=list)
    preferred_stacks = Column(JSON, nullable=True, default=list)
    quality_priorities = Column(JSON, nullable=True, default=list)
    auto_apply_level = Column(Integer, nullable=False, default=1)  # 1=CRITICAL, 2=CORE, 3=ADVANCED
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())

class ImprovementProgressModel(Base):
    """Track real-time improvement progress"""
    __tablename__ = "improvement_progress"
    
    id = Column(String, primary_key=True)
    project_id = Column(String, nullable=False, index=True)
    user_id = Column(String, nullable=True)
    status = Column(String, nullable=False)  # started, in_progress, completed, failed
    current_iteration = Column(Integer, default=0)
    max_iterations = Column(Integer, default=5)
    target_quality = Column(Float, default=0.85)
    current_quality = Column(Float, default=0.0)
    improvements_applied = Column(Integer, default=0)
    progress_data = Column(JSON, nullable=True)
    error_message = Column(String, nullable=True)
    started_at = Column(DateTime, default=func.now())
    completed_at = Column(DateTime, nullable=True)
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())