"""Project model."""
import json
from datetime import datetime
from sqlalchemy import Column, Integer, String, Text, DateTime

from ..database import Base


class Project(Base):
    """A simulation project containing multiple plans and results."""
    __tablename__ = "projects"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(200), nullable=False, index=True)
    description = Column(Text, default="")
    topology = Column(String(20), default="SSSR")  # SSSR / DRSS / SDSR
    model_path = Column(String(500), default="")
    status = Column(String(20), default="draft")  # draft / active / completed / archived
    boundary_conditions = Column(Text, default="{}")  # JSON: dimensions, targets, constraints
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

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

    def set_boundary_conditions(self, data: dict) -> None:
        self.boundary_conditions = json.dumps(data, ensure_ascii=False)
