from __future__ import annotations import uuid from datetime import datetime from typing import Any from sqlalchemy import JSON, Boolean, DateTime, Enum, Float, ForeignKey, Integer, String from sqlalchemy.orm import Mapped, mapped_column from app.db.base import Base, TimestampMixin, UUIDPrimaryKeyMixin from app.models.enums import MonitoringFrequency, SourceStatus, SourceType class Source(Base, UUIDPrimaryKeyMixin, TimestampMixin): __tablename__ = "sources" company_id: Mapped[uuid.UUID] = mapped_column( ForeignKey("companies.id", ondelete="CASCADE"), index=True ) source_type: Mapped[SourceType] = mapped_column(Enum(SourceType, native_enum=False, length=20)) name: Mapped[str] = mapped_column(String(200)) base_url: Mapped[str | None] = mapped_column(String(500), nullable=True) active: Mapped[bool] = mapped_column(Boolean, default=True) status: Mapped[SourceStatus] = mapped_column( Enum(SourceStatus, native_enum=False, length=20), default=SourceStatus.ACTIVE ) trust_score: Mapped[float] = mapped_column(Float, default=0.7) last_checked: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) last_successful_check: Mapped[datetime | None] = mapped_column( DateTime(timezone=True), nullable=True ) failure_count: Mapped[int] = mapped_column(Integer, default=0) configuration_metadata: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict) # Per-source check cadence override - NULL frequency_type means "inherit # the company's default MonitorConfiguration cadence" (the behavior for # every source before this feature existed, and still the default for # any source that never sets an override). See app/tasks/scheduler.py # and app/services/scheduling.py for how these combine into due-ness. frequency_type: Mapped[MonitoringFrequency | None] = mapped_column( Enum(MonitoringFrequency, native_enum=False, length=20), nullable=True ) interval_minutes: Mapped[int | None] = mapped_column(Integer, nullable=True) cron_expression: Mapped[str | None] = mapped_column(String(120), nullable=True) next_check: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)