import type { ApiSettings } from "../types";
import { apiFetch, apiPath } from "./client";
import type {
  AccessDecisionProvenanceItem,
  ResourceAccessExplanationOptions,
  ResourceAccessExplanationResponse,
  ResourceAccessExplanationUser
} from "./resourceAccessContracts";
export type {
  AccessDecisionProvenanceItem,
  ResourceAccessExplanationOptions,
  ResourceAccessExplanationResponse,
  ResourceAccessExplanationUser
} from "./resourceAccessContracts";

/**
 * Fetches the platform's optional resource-access explanation endpoint.
 * Core owns the transport contract without requiring the Access module that
 * contributes the endpoint at runtime.
 */
export function fetchResourceAccessExplanation<
  TUser extends ResourceAccessExplanationUser = ResourceAccessExplanationUser,
  TProvenance extends AccessDecisionProvenanceItem = AccessDecisionProvenanceItem
>(
  settings: ApiSettings,
  options: ResourceAccessExplanationOptions
): Promise<ResourceAccessExplanationResponse<TUser, TProvenance>> {
  return apiFetch(settings, apiPath("/api/v1/admin/access/resource-explanation", {
    user_id: options.userId,
    resource_type: options.resourceType,
    resource_id: options.resourceId,
    action: options.action,
    tenant_id: options.tenantId
  }));
}
