mod common;

use common::mock_client;
use ticksupply::resources::billing::AccessStatus;
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};

#[tokio::test]
async fn summary_decodes_trialing() {
    let (server, client) = mock_client().await;
    Mock::given(method("GET"))
        .and(path("/v1/billing/summary"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "access_status": "TRIALING",
            "plan_code": "trial",
            "current_period_end": "2026-05-01T00:00:00Z",
            "usage": {
                "extra_stream_minutes_total": 0,
                "export_gb_total": "0.0",
                "export_gb_extra": 0
            }
        })))
        .mount(&server)
        .await;

    let s = client.billing().summary().await.unwrap();
    assert_eq!(s.access_status, AccessStatus::Trialing);
    assert_eq!(s.plan_code.as_deref(), Some("trial"));
    assert_eq!(s.usage.extra_stream_minutes_total, 0);
    assert_eq!(s.usage.export_gb_total.as_str(), "0.0");
}

#[tokio::test]
async fn summary_decodes_grace_with_grace_until() {
    let (server, client) = mock_client().await;
    Mock::given(method("GET"))
        .and(path("/v1/billing/summary"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "access_status": "GRACE",
            "grace_until": "2026-05-07T12:00:00Z",
            "plan_code": "pro",
            "current_period_end": "2026-05-01T00:00:00Z",
            "usage": {
                "extra_stream_minutes_total": 42,
                "export_gb_total": "12.75",
                "export_gb_extra": 3
            }
        })))
        .mount(&server)
        .await;

    let s = client.billing().summary().await.unwrap();
    assert_eq!(s.access_status, AccessStatus::Grace);
    assert_eq!(
        s.grace_until.as_ref().map(|t| t.as_str()),
        Some("2026-05-07T12:00:00Z")
    );
    assert_eq!(s.usage.extra_stream_minutes_total, 42);
    assert_eq!(s.usage.export_gb_total.as_str(), "12.75");
    assert_eq!(s.usage.export_gb_total.to_f64(), Some(12.75));
    assert_eq!(s.usage.export_gb_extra, 3);
}
