use serde::{Deserialize, Serialize};

/// Query-string pagination for list endpoints. The Flutter client at
/// flutter/lib/models/ab_model.dart and group_model.dart sends
/// `?current=1&pageSize=100` against every paginated list. Field names are
/// spelled explicitly here — `serde(rename_all = "camelCase")` would also
/// rename `current`, which we don't want.
#[derive(Debug, Deserialize)]
pub struct PageQuery {
    #[serde(default = "default_current")]
    pub current: i64,
    #[serde(default = "default_page_size", rename = "pageSize")]
    pub page_size: i64,
}

fn default_current() -> i64 {
    1
}
fn default_page_size() -> i64 {
    100
}

impl PageQuery {
    pub fn offset(&self) -> i64 {
        let cur = self.current.max(1);
        (cur - 1) * self.limit()
    }
    pub fn limit(&self) -> i64 {
        self.page_size.clamp(1, 1000)
    }
}

/// Standard envelope: `{ total, data }`.
#[derive(Debug, Serialize)]
pub struct Page<T: Serialize> {
    pub total: i64,
    pub data: Vec<T>,
}
