use async_trait::async_trait;
use salvo::oapi::ToSchema;
use serde::{Deserialize, Serialize};

/// Shared external identifiers for a media item.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, ToSchema)]
pub struct ExternalIds {
    /// IMDb title identifier such as `tt1234567`.
    pub imdb_id: Option<String>,
    /// TMDb identifier.
    pub tmdb_id: Option<i32>,
    /// TVDb identifier.
    pub tvdb_id: Option<i32>,
    /// TVMaze identifier.
    pub tvmaze_id: Option<i32>,
    /// Trakt identifier.
    pub trakt_id: Option<i32>,
    /// Douban identifier.
    pub douban_id: Option<String>,
}

/// Minimal pagination value object shared across provider query boundaries.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, ToSchema)]
pub struct Pagination {
    /// Zero-based result offset.
    pub offset: usize,
    /// Maximum number of items requested.
    pub limit: usize,
}

impl Default for Pagination {
    fn default() -> Self {
        Self {
            offset: 0,
            limit: 20,
        }
    }
}

/// Base trait shared by all provider types.
#[async_trait]
pub trait Provider: Send + Sync {
    /// Return the stable provider key used in logs and routing.
    fn key(&self) -> &str;

    /// Return the provider display name.
    fn name(&self) -> &str;

    /// Return a baseline provider health snapshot.
    async fn health_check(&self) -> ProviderStatus;
}

/// Health status of a provider.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ProviderStatus {
    /// The provider responded successfully.
    Healthy {
        /// Measured latency in milliseconds.
        latency_ms: u64,
    },
    /// The provider did not respond successfully.
    Unhealthy,
}

/// Named provider health entry returned by provider hubs.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ProviderStatusEntry {
    /// Provider key.
    pub key: String,
    /// Provider name.
    pub name: String,
    /// Health snapshot.
    pub status: ProviderStatus,
}
