"""
Schedule Models
"""
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey
from sqlalchemy.sql import func
from app.core.database import Base


class ScheduleType(Base):
    """Schedule types lookup table"""
    __tablename__ = "schedule_types"
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(Text, nullable=False, unique=True)
    description = Column(Text, nullable=False)
    created_at = Column(DateTime, default=func.now())


class ScheduledJob(Base):
    """Scheduled jobs table"""
    __tablename__ = "scheduled_jobs"
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Text, nullable=False)
    agent_id = Column(Text, nullable=False)
    username = Column(Text, nullable=False)
    question = Column(Text, nullable=False)
    question_id = Column(Text, nullable=False)
    schedule_type_id = Column(Integer, ForeignKey("schedule_types.id"), nullable=False)
    cron_expression = Column(Text, nullable=False)
    distribution_list_id = Column(Integer, ForeignKey("distribution_lists.id"), nullable=False)
    status = Column(Text, default="active")
    start_date = Column(DateTime, nullable=False)
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())


class ExecutionLog(Base):
    """Job execution log table"""
    __tablename__ = "execution_log"
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    scheduled_job_id = Column(Integer, ForeignKey("scheduled_jobs.id", ondelete="CASCADE"), nullable=False)
    user_id = Column(Text, nullable=False)
    agent_id = Column(Text, nullable=False)
    question = Column(Text, nullable=False)
    execution_time = Column(DateTime, nullable=False)
    status = Column(Text, nullable=False)
    api_params = Column(Text, nullable=False)
    api_response_validator = Column(Text, nullable=False)
    created_at = Column(DateTime, default=func.now())
