use serde::{Deserialize, Serialize};

// =============================================================================
// Shared nested types
// =============================================================================

/// Minimal id-only reference used inside player/transaction/team responses.
#[derive(Debug, Deserialize)]
pub struct IdRef {
    pub id: i64,
}

/// Manager sub-object nested inside TeamData.
#[derive(Debug, Deserialize)]
pub struct Manager {
    pub name: Option<String>,
}

/// Division sub-object nested inside TeamData.
#[derive(Debug, Deserialize)]
pub struct Division {
    pub id: Option<i64>,
    pub division_name: Option<String>,
    pub league_abbrev: Option<String>,
}

// =============================================================================
// Teams
// =============================================================================

#[derive(Debug, Deserialize)]
pub struct TeamsResponse {
    pub count: u32,
    pub teams: Vec<TeamData>,
}

#[derive(Debug, Deserialize)]
pub struct TeamData {
    pub id: i64,
    pub abbrev: Option<String>,
    #[serde(rename = "sname")]
    pub short_name: Option<String>,
    #[serde(rename = "lname")]
    pub long_name: Option<String>,
    pub thumbnail: Option<String>,
    pub color: Option<String>,
    pub dice_color: Option<String>,
    pub stadium: Option<String>,
    pub salary_cap: Option<f64>,
    /// Discord user ID of the primary GM (API sends as string).
    #[serde(rename = "gmid")]
    pub gm_discord_id: Option<String>,
    /// Discord user ID of the secondary GM (API sends as string).
    #[serde(rename = "gmid2")]
    pub gm2_discord_id: Option<String>,
    pub manager1: Option<Manager>,
    pub manager2: Option<Manager>,
    pub division: Option<Division>,
}

// =============================================================================
// Players
// =============================================================================

#[derive(Debug, Deserialize)]
pub struct PlayersResponse {
    pub count: u32,
    pub players: Vec<PlayerData>,
}

#[derive(Debug, Deserialize)]
pub struct PlayerData {
    pub id: i64,
    pub name: Option<String>,
    pub headshot: Option<String>,
    pub vanity_card: Option<String>,
    /// Strat-O-Matic WAR equivalent — API field is "wara".
    #[serde(rename = "wara")]
    pub swar: Option<f64>,
    /// SBA player ID — API field is "sbaplayer".
    #[serde(rename = "sbaplayer")]
    pub sbaplayer_id: Option<i64>,
    /// Primary card image URL — API field is "image".
    #[serde(rename = "image")]
    pub card_image: Option<String>,
    /// Alternate card image URL — API field is "image2".
    #[serde(rename = "image2")]
    pub card_image_alt: Option<String>,
    pub team: Option<IdRef>,
    pub pos_1: Option<String>,
    pub pos_2: Option<String>,
    pub pos_3: Option<String>,
    pub pos_4: Option<String>,
    pub pos_5: Option<String>,
    pub pos_6: Option<String>,
    pub pos_7: Option<String>,
    pub pos_8: Option<String>,
    pub injury_rating: Option<String>,
    pub il_return: Option<String>,
    pub demotion_week: Option<i64>,
    pub strat_code: Option<String>,
    pub bbref_id: Option<String>,
    pub last_game: Option<String>,
    pub last_game2: Option<String>,
}

// =============================================================================
// Transactions
// =============================================================================

#[derive(Debug, Deserialize)]
pub struct TransactionsResponse {
    pub count: u32,
    pub transactions: Vec<TransactionData>,
}

#[derive(Debug, Deserialize)]
pub struct TransactionData {
    /// Transaction move ID — API field is "moveid" (string like "Season-013-Week-11-1772073335").
    #[serde(rename = "moveid")]
    pub move_id: Option<String>,
    pub week: Option<i64>,
    pub cancelled: Option<bool>,
    pub frozen: Option<bool>,
    pub player: Option<IdRef>,
    pub oldteam: Option<IdRef>,
    pub newteam: Option<IdRef>,
}

// =============================================================================
// Current season info
// =============================================================================

#[derive(Debug, Deserialize)]
pub struct CurrentResponse {
    pub season: i64,
    pub week: i64,
}

// =============================================================================
// Standings
// =============================================================================

#[derive(Debug, Deserialize, Serialize)]
pub struct StandingsTeamRef {
    pub id: i64,
    pub abbrev: Option<String>,
    #[serde(rename = "sname")]
    pub short_name: Option<String>,
    #[serde(rename = "lname")]
    pub long_name: Option<String>,
    pub division: Option<StandingsDivision>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct StandingsDivision {
    pub id: Option<i64>,
    pub division_name: Option<String>,
    pub division_abbrev: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct StandingsResponse {
    pub count: u32,
    pub standings: Vec<StandingsEntry>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct StandingsEntry {
    pub team: StandingsTeamRef,
    #[serde(default)]
    pub wins: i64,
    #[serde(default)]
    pub losses: i64,
    #[serde(default)]
    pub run_diff: i64,
    pub div_gb: Option<f64>,
    pub wc_gb: Option<f64>,
    #[serde(default)]
    pub home_wins: i64,
    #[serde(default)]
    pub home_losses: i64,
    #[serde(default)]
    pub away_wins: i64,
    #[serde(default)]
    pub away_losses: i64,
    #[serde(default)]
    pub last8_wins: i64,
    #[serde(default)]
    pub last8_losses: i64,
    pub streak_wl: Option<String>,
    #[serde(default)]
    pub streak_num: i64,
    #[serde(default)]
    pub one_run_wins: i64,
    #[serde(default)]
    pub one_run_losses: i64,
    #[serde(default)]
    pub pythag_wins: i64,
    #[serde(default)]
    pub pythag_losses: i64,
}
