"""Experience case model (for knowledge base / similar case retrieval)."""
import json
from datetime import datetime
from sqlalchemy import Column, Integer, String, Text, DateTime, Float

from ..database import Base


class ExperienceCase(Base):
    """A structured experience case for similarity retrieval."""
    __tablename__ = "experience_cases"

    id = Column(Integer, primary_key=True, index=True)
    source_plan_id = Column(String(100), default="")
    topology = Column(String(20), default="SSSR", index=True)
    model_path = Column(String(500), default="")
    params_json = Column(Text, default="{}")  # JSON: parameter values
    metrics_json = Column(Text, default="{}")  # JSON: metric values
    boundary_json = Column(Text, default="{}")  # JSON: boundary conditions snapshot
    conclusion = Column(Text, default="")  # Human/AI generated conclusion
    tags = Column(String(500), default="")  # Comma-separated tags
    rating = Column(Integer, default=0)  # 0-5, expert rating
    created_at = Column(DateTime, default=datetime.utcnow)

    def get_params(self) -> dict:
        try:
            return json.loads(self.params_json) if self.params_json else {}
        except (json.JSONDecodeError, TypeError):
            return {}

    def get_metrics(self) -> dict:
        try:
            return json.loads(self.metrics_json) if self.metrics_json else {}
        except (json.JSONDecodeError, TypeError):
            return {}
