"""Simulation task model for Web-Local dispatch (P4-M2)."""
from sqlalchemy import Column, Integer, String, Float, DateTime, Text
from sqlalchemy.sql import func

from ..database import Base


class Task(Base):
    """Simulation task dispatched from Web to Local executor."""

    __tablename__ = "tasks"

    id = Column(Integer, primary_key=True, index=True)
    task_id = Column(String(16), unique=True, index=True, nullable=False)
    task_name = Column(String(200), nullable=False)
    plan_id = Column(Integer, nullable=True)
    status = Column(String(20), default="pending", index=True)
    priority = Column(Integer, default=5)
    total_points = Column(Integer, default=0)
    completed_points = Column(Integer, default=0)
    task_dir = Column(String(500), nullable=True)
    task_file = Column(String(500), nullable=True)
    results_file = Column(String(500), nullable=True)
    progress_data = Column(Text, nullable=True)
    result_metrics = Column(Text, nullable=True)
    created_by = Column(String(50), default="web")
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    dispatched_at = Column(DateTime(timezone=True), nullable=True)
    started_at = Column(DateTime(timezone=True), nullable=True)
    completed_at = Column(DateTime(timezone=True), nullable=True)
    duration = Column(Float, nullable=True)
