use anyhow::Result;

/// Steam App ID for the Rust Dedicated Server.
pub const RUST_SERVER_APP_ID: u32 = 258550;

/// Steam build ID polling service.
///
/// Periodically checks the Steam Web API for new Rust Dedicated Server
/// builds. When a new build is detected (build ID changes), it publishes
/// an event to NATS so that servers configured for auto-update-on-force-wipe
/// can be notified and trigger their wipe schedules.
pub struct SteamUpdateWatcher {
    // TODO: Add fields:
    // - nats: async_nats::Client
    // - db: sqlx::PgPool
    // - poll_interval: std::time::Duration
    // - last_known_build_id: Option<String>
}

impl SteamUpdateWatcher {
    /// Start the polling loop. Runs indefinitely, checking for updates
    /// at the configured interval.
    pub async fn start_polling(&self) -> Result<()> {
        // TODO: Loop on poll_interval
        // TODO: Call check_for_update each iteration
        // TODO: If new build detected, call handle_update_detected
        todo!()
    }

    /// Check Steam Web API for the current build ID of the Rust server app.
    ///
    /// Returns the build ID string if an update is detected (differs from
    /// last known), or None if unchanged.
    pub async fn check_for_update(&self) -> Result<Option<String>> {
        // TODO: GET https://api.steampowered.com/ISteamApps/UpToDateCheck/v1/
        //   ?appid={RUST_SERVER_APP_ID}&version=0
        // TODO: Parse response for build ID
        // TODO: Compare against last_known_build_id
        // TODO: Return Some(new_id) if changed, None if same
        todo!()
    }

    /// Handle a detected Steam update: persist the new build ID,
    /// publish NATS event for downstream consumers.
    pub async fn handle_update_detected(&self, _new_build_id: &str) -> Result<()> {
        // TODO: Update last_known_build_id
        // TODO: Persist to DB for crash recovery
        // TODO: Publish to NATS subject corrosion.steam.update_detected
        //   with payload { app_id, old_build_id, new_build_id, detected_at }
        // TODO: Log the detection
        todo!()
    }
}
