"""ORM models for the universal Fetch Data Toggle system.

Two tables back the feature:

* :class:`ModuleSyncSettings` — one row per module (``uptimerobot``,
  ``bizmail360``, ``whatsapp``). Holds the three toggle flags plus the
  latest sync timestamp, status string, error message, and running
  fetched-count totals for clients/accounts/domains.

* :class:`ModuleSyncLog` — append-only history of sync attempts
  (manual, cron, or diagnostic). Used by the "View Raw API Log" admin UI.

Both tables are created by the Alembic revision ``m1s2y3n4c5e6``.
"""
from __future__ import annotations

from sqlalchemy import Boolean, Column, DateTime, Integer, String, Text
from sqlalchemy.sql import func

from models.base import Base


class ModuleSyncSettings(Base):
    __tablename__ = "module_sync_settings"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    module_name = Column(String(64), nullable=False, unique=True, index=True)

    fetch_clients_enabled = Column(Boolean, nullable=False, default=False)
    fetch_accounts_enabled = Column(Boolean, nullable=False, default=False)
    fetch_domains_enabled = Column(Boolean, nullable=False, default=False)

    last_clients_sync = Column(DateTime(timezone=True), nullable=True)
    last_accounts_sync = Column(DateTime(timezone=True), nullable=True)
    last_domains_sync = Column(DateTime(timezone=True), nullable=True)

    clients_sync_status = Column(String(32), nullable=False, default="never")
    accounts_sync_status = Column(String(32), nullable=False, default="never")
    domains_sync_status = Column(String(32), nullable=False, default="never")

    clients_count = Column(Integer, nullable=False, default=0)
    accounts_count = Column(Integer, nullable=False, default=0)
    domains_count = Column(Integer, nullable=False, default=0)
    domains_linked_count = Column(Integer, nullable=False, default=0)
    domains_unlinked_count = Column(Integer, nullable=False, default=0)

    last_error = Column(Text, nullable=True)

    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    updated_at = Column(
        DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
    )


class ModuleSyncLog(Base):
    __tablename__ = "module_sync_logs"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    module_name = Column(String(64), nullable=False, index=True)
    # 'clients' | 'accounts' | 'domains' | 'diagnostic'
    sync_type = Column(String(32), nullable=False, index=True)
    # 'ok' | 'failed' | 'skipped'
    status = Column(String(32), nullable=False)
    items_fetched = Column(Integer, nullable=False, default=0)
    error_message = Column(Text, nullable=True)
    raw_response = Column(Text, nullable=True)
    # 'manual' | 'cron' | 'diagnostic'
    triggered_by = Column(String(32), nullable=False, default="manual")
    actor_email = Column(String(255), nullable=True)
    duration_ms = Column(Integer, nullable=False, default=0)

    started_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False, index=True)
    finished_at = Column(DateTime(timezone=True), nullable=True)