//! Sync-state enums derived per-manga / per-chapter from `manga_sources`,
//! `chapter_sources`, and `crawler_jobs` at query time. No state column
//! is persisted on `mangas` / `chapters` — see `repo::admin_view` for the
//! derivation rules and priority order.

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, sqlx::Type)]
#[sqlx(type_name = "text", rename_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum MangaSyncState {
    /// A `sync_manga` or `sync_chapter_list` job is currently
    /// pending or running for this manga.
    InProgress,
    /// At least one `manga_sources` row exists for this manga and ALL of
    /// them have `dropped_at IS NOT NULL` — every source we know about
    /// has stopped surfacing it.
    Dropped,
    /// Default healthy state: at least one live source row OR the manga
    /// was user-uploaded (no `manga_sources` rows at all).
    Synced,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, sqlx::Type)]
#[sqlx(type_name = "text", rename_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum ChapterSyncState {
    /// A `sync_chapter_content` job is currently pending or running for
    /// this chapter (the 0014 dedup index guarantees at most one).
    Downloading,
    /// At least one `chapter_sources` row exists AND all of them are
    /// `dropped_at IS NOT NULL`.
    Dropped,
    /// `page_count = 0` AND a `dead` `sync_chapter_content` job exists
    /// for this chapter. Checked BEFORE `NotDownloaded` so the more
    /// informative "we tried and it died" state wins over "we never
    /// got around to it". Does NOT fire when `page_count > 0`, because
    /// pages on disk mean the chapter IS synced regardless of historical
    /// job failures — see the priority comment in `repo::admin_view`.
    Failed,
    /// `page_count = 0` and no in-flight or failed job — the chapter
    /// row exists but content has never been downloaded.
    NotDownloaded,
    /// `page_count > 0` — content has been downloaded at some point.
    /// Reaped `done` jobs in `crawler_jobs` mean we can't read this from
    /// the job table, so `page_count` is the durable truth.
    Synced,
}
