from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, JSON
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
import datetime

Base = declarative_base()

class Queue(Base):
    __tablename__ = "queues"

    id = Column(String, primary_key=True, index=True)
    status = Column(String, default="Waiting") # Finished, Waiting, Running, Aborted
    created_at = Column(DateTime, default=datetime.datetime.utcnow)
    environment = Column(String)
    
    tasks = relationship("Task", back_populates="queue", cascade="all, delete-orphan")

class Task(Base):
    __tablename__ = "tasks"

    id = Column(String, primary_key=True, index=True)
    queue_id = Column(String, ForeignKey("queues.id"))
    scenario_path = Column(String)
    status = Column(String, default="Waiting") # Finished, Waiting, Running, Aborted
    result = Column(JSON, nullable=True)
    
    queue = relationship("Queue", back_populates="tasks")
