use dioxus::prelude::*;
use serde::{Deserialize, Serialize};

use compliance_core::models::TrackerIssue;

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct IssuesListResponse {
    pub data: Vec<TrackerIssue>,
    pub total: Option<u64>,
    pub page: Option<u64>,
}

#[server]
pub async fn fetch_issues(page: u64) -> Result<IssuesListResponse, ServerFnError> {
    let resp = super::agent_client::agent_get(&format!("/api/v1/issues?page={page}&limit=20"))
        .await?
        .send()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;
    let body: IssuesListResponse = resp
        .json()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;
    Ok(body)
}
