import { apiFetch, apiPath, type ApiSettings } from "@govoplan/core-webui"; export type ServiceDeskDiagnostic = { severity: "info" | "warning" | "error"; code: string; message: string; object_ref?: string | null; field?: string | null; retryable: boolean; details: Record; }; export type ServiceDeskRouteMapping = { search_path: string; ticket_path: string; update_path?: string | null; search_method: "GET" | "POST"; ticket_method: "GET" | "POST"; update_method: "PATCH" | "POST" | "PUT"; ticket_web_url_template?: string | null; search_filters: Record; }; export type ServiceDeskQueueMapping = { source_queue: string; target_queue_ref?: string | null; include: boolean; visibility: "tenant" | "restricted"; acl_tokens: string[]; }; export type ServiceDeskDynamicFieldMapping = { source_name: string; target_name?: string | null; include: boolean; value_type: "string" | "number" | "boolean" | "date" | "json"; }; export type ServiceDeskProfile = { id: string; tenant_id: string; configuration_id: string; status: "active" | "paused"; integration_mode: "link" | "import" | "synchronize"; product: string; product_version?: string | null; desired_maturity: "discover" | "link" | "search" | "read" | "publish" | "synchronize"; discovered_maturity: string; source_authority_mode: "external_authoritative" | "external_mirror" | "governed_sync" | "linked_reference"; default_visibility: "tenant" | "restricted"; default_acl_tokens: string[]; routes: ServiceDeskRouteMapping; queue_mappings: ServiceDeskQueueMapping[]; dynamic_field_mappings: ServiceDeskDynamicFieldMapping[]; capabilities: string[]; discovery_revision?: string | null; discovered_configuration_revision?: number | null; discovered_configuration_hash?: string | null; health_status: string; health_details: Record; discovered_at?: string | null; last_sync_cursor?: string | null; last_high_watermark?: string | null; resource_revision: number; credential_reference_present: boolean; endpoint_configured: boolean; created_at: string; updated_at: string; }; export type ServiceDeskObject = { id: string; profile_id: string; object_type: string; external_id: string; external_ticket_number?: string | null; title: string; canonical_url?: string | null; status: string; source_revision: string; visibility: string; acl_tokens: string[]; mapped_data: Record; provenance: Record; source_updated_at?: string | null; observed_at: string; resource_revision: number; }; export type ServiceDeskRun = { id: string; profile_id: string; mode: string; idempotency_key: string; status: string; cursor_before?: string | null; cursor_after?: string | null; high_watermark?: string | null; counts: Record; effects: Array>; diagnostics: ServiceDeskDiagnostic[]; provenance: Record; started_at: string; finished_at?: string | null; created_at: string; }; export type ServiceDeskDiscovery = { profile: ServiceDeskProfile; product: string; product_version?: string | null; api_family: string; capabilities: string[]; maturity: string; health_status: string; diagnostics: ServiceDeskDiagnostic[]; revision: string; }; export type ServiceDeskTicketUpdateResult = { run: ServiceDeskRun; object: ServiceDeskObject; accepted: boolean; outcome_unknown: boolean; }; const ROOT = "/api/v1/connectors/service-desk"; export async function listServiceDeskProfiles(settings: ApiSettings): Promise { const response = await apiFetch<{ items: ServiceDeskProfile[] }>(settings, `${ROOT}/profiles`); return response.items; } export function createServiceDeskProfile( settings: ApiSettings, payload: Record ): Promise { return apiFetch(settings, `${ROOT}/profiles`, { method: "POST", body: JSON.stringify(payload) }); } export function updateServiceDeskProfile( settings: ApiSettings, profileId: string, payload: Record ): Promise { return apiFetch(settings, `${ROOT}/profiles/${encodeURIComponent(profileId)}`, { method: "PUT", body: JSON.stringify(payload) }); } export function discoverServiceDeskProfile( settings: ApiSettings, profileId: string ): Promise { return apiFetch(settings, `${ROOT}/profiles/${encodeURIComponent(profileId)}/discover`, { method: "POST" }); } export function synchronizeServiceDeskProfile( settings: ApiSettings, profileId: string, payload: Record ): Promise { return apiFetch(settings, `${ROOT}/profiles/${encodeURIComponent(profileId)}/sync`, { method: "POST", body: JSON.stringify(payload) }); } export async function listServiceDeskObjects( settings: ApiSettings, profileId: string ): Promise { const response = await apiFetch<{ items: ServiceDeskObject[] }>( settings, apiPath(`${ROOT}/profiles/${encodeURIComponent(profileId)}/objects`, { limit: 100 }) ); return response.items; } export async function listServiceDeskRuns( settings: ApiSettings, profileId?: string ): Promise { const response = await apiFetch<{ items: ServiceDeskRun[] }>( settings, apiPath(`${ROOT}/runs`, { profile_id: profileId || undefined, limit: 100 }) ); return response.items; } export function updateServiceDeskTicket( settings: ApiSettings, profileId: string, externalTicketId: string, payload: Record ): Promise { return apiFetch( settings, `${ROOT}/profiles/${encodeURIComponent(profileId)}/tickets/${encodeURIComponent(externalTicketId)}/update`, { method: "POST", body: JSON.stringify(payload) } ); }