"""
ScanSchedule Model für automatische Scan-Jobs
"""
from enum import Enum
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Enum as SQLEnum
from app.models.base import Base, TimestampMixin


class ScheduleInterval(str, Enum):
    """Intervall-Optionen für geplante Scans"""
    EVERY_HOUR = "every_hour"
    EVERY_6_HOURS = "every_6_hours"
    EVERY_12_HOURS = "every_12_hours"
    DAILY = "daily"
    WEEKLY = "weekly"
    CRON = "cron"


class ScanSchedule(Base, TimestampMixin):
    """
    ScanSchedule-Tabelle für automatische Scan-Planung

    Ermöglicht periodische automatische Scans aller Wazuh-Assets
    """
    __tablename__ = "scan_schedules"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(200), nullable=False)
    interval = Column(
        SQLEnum(ScheduleInterval),
        nullable=False,
        default=ScheduleInterval.DAILY
    )
    cron_expression = Column(String(100), nullable=True) # e.g. "0 2 * * *" for 2:00 AM
    # Which sync backend this schedule should drive. Defaults to 'wazuh'
    # for backwards compatibility with pre-Nessus schedules.
    scanner_type = Column(String(20), nullable=False, default="wazuh", server_default="wazuh")
    enabled = Column(Boolean, default=True, nullable=False)
    last_run = Column(DateTime, nullable=True)
    next_run = Column(DateTime, nullable=True)
    created_by = Column(Integer, nullable=True)

    def __repr__(self):
        return f"<ScanSchedule(name='{self.name}', interval='{self.interval}', enabled={self.enabled})>"
