use reqwest::Method;

use super::Client;
use crate::error::Error;
use crate::types::{
    LicenseActivatePayload, LicenseActivateResponse, LicenseCheckPayload, LicenseCheckResponse,
    LicensePublicKeysResponse, LicenseRefreshPayload, LicenseSyncUsagePayload,
    LicenseSyncUsageResponse,
};

impl Client {
    /// POST /api/licenses/check - runtime feature gate check.
    pub async fn license_check(
        &self,
        payload: LicenseCheckPayload<'_>,
    ) -> Result<LicenseCheckResponse, Error> {
        self.do_request::<_, LicenseCheckResponse>(
            Method::POST,
            "/api/licenses/check",
            Some(&payload),
        )
        .await
    }

    /// POST /api/licenses/activate - activate device, returns signed license envelope.
    pub async fn license_activate(
        &self,
        payload: LicenseActivatePayload<'_>,
    ) -> Result<LicenseActivateResponse, Error> {
        self.do_request::<_, LicenseActivateResponse>(
            Method::POST,
            "/api/licenses/activate",
            Some(&payload),
        )
        .await
    }

    /// POST /api/licenses/refresh - refresh signed license envelope.
    pub async fn license_refresh(
        &self,
        payload: LicenseRefreshPayload<'_>,
    ) -> Result<LicenseActivateResponse, Error> {
        self.do_request::<_, LicenseActivateResponse>(
            Method::POST,
            "/api/licenses/refresh",
            Some(&payload),
        )
        .await
    }

    pub async fn license_sync_usage(
        &self,
        payload: LicenseSyncUsagePayload<'_>,
    ) -> Result<LicenseSyncUsageResponse, Error> {
        self.do_request::<_, LicenseSyncUsageResponse>(
            Method::POST,
            "/api/licenses/sync-usage",
            Some(&payload),
        )
        .await
    }

    /// GET /api/v1/license-keys/public - list registered Ed25519 verification keys.
    /// Public endpoint, no HMAC or bearer required.
    pub async fn public_license_keys(&self) -> Result<LicensePublicKeysResponse, Error> {
        self.get_public::<LicensePublicKeysResponse>("/api/v1/license-keys/public")
            .await
    }
}
