/**
 * DSR API Workflow Actions
 *
 * Workflow operations: identity verification, assignment, deadline extension,
 * completion, rejection, communications, exception checks, and history.
 */

import { DSRRequest } from './types'
import { transformBackendDSR, getSdkHeaders } from './api-types'

// =============================================================================
// WORKFLOW ACTIONS
// =============================================================================

/**
 * Verify identity of DSR requester
 */
export async function verifyDSRIdentity(id: string, data: { method: string; notes?: string; document_ref?: string }): Promise<DSRRequest> {
  const res = await fetch(`/api/sdk/v1/compliance/dsr/${id}/verify-identity`, {
    method: 'POST',
    headers: getSdkHeaders(),
    body: JSON.stringify(data),
  })
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  return transformBackendDSR(await res.json())
}

/**
 * Assign DSR to a user
 */
export async function assignDSR(id: string, assigneeId: string): Promise<DSRRequest> {
  const res = await fetch(`/api/sdk/v1/compliance/dsr/${id}/assign`, {
    method: 'POST',
    headers: getSdkHeaders(),
    body: JSON.stringify({ assignee_id: assigneeId }),
  })
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  return transformBackendDSR(await res.json())
}

/**
 * Extend DSR deadline (Art. 12 Abs. 3 DSGVO)
 */
export async function extendDSRDeadline(id: string, reason: string, days: number = 60): Promise<DSRRequest> {
  const res = await fetch(`/api/sdk/v1/compliance/dsr/${id}/extend`, {
    method: 'POST',
    headers: getSdkHeaders(),
    body: JSON.stringify({ reason, days }),
  })
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  return transformBackendDSR(await res.json())
}

/**
 * Complete a DSR
 */
export async function completeDSR(id: string, summary?: string): Promise<DSRRequest> {
  const res = await fetch(`/api/sdk/v1/compliance/dsr/${id}/complete`, {
    method: 'POST',
    headers: getSdkHeaders(),
    body: JSON.stringify({ summary }),
  })
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  return transformBackendDSR(await res.json())
}

/**
 * Reject a DSR with legal basis
 */
export async function rejectDSR(id: string, reason: string, legalBasis?: string): Promise<DSRRequest> {
  const res = await fetch(`/api/sdk/v1/compliance/dsr/${id}/reject`, {
    method: 'POST',
    headers: getSdkHeaders(),
    body: JSON.stringify({ reason, legal_basis: legalBasis }),
  })
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  return transformBackendDSR(await res.json())
}

// =============================================================================
// COMMUNICATIONS
// =============================================================================

/**
 * Fetch communications for a DSR
 */
export async function fetchDSRCommunications(id: string): Promise<any[]> {
  const res = await fetch(`/api/sdk/v1/compliance/dsr/${id}/communications`, {
    headers: getSdkHeaders(),
  })
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  return res.json()
}

/**
 * Send a communication for a DSR
 */
export async function sendDSRCommunication(id: string, data: { communication_type?: string; channel?: string; subject?: string; content: string }): Promise<any> {
  const res = await fetch(`/api/sdk/v1/compliance/dsr/${id}/communicate`, {
    method: 'POST',
    headers: getSdkHeaders(),
    body: JSON.stringify({ communication_type: 'outgoing', channel: 'email', ...data }),
  })
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  return res.json()
}

// =============================================================================
// EXCEPTION CHECKS (Art. 17)
// =============================================================================

/**
 * Fetch exception checks for an erasure DSR
 */
export async function fetchDSRExceptionChecks(id: string): Promise<any[]> {
  const res = await fetch(`/api/sdk/v1/compliance/dsr/${id}/exception-checks`, {
    headers: getSdkHeaders(),
  })
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  return res.json()
}

/**
 * Initialize Art. 17(3) exception checks for an erasure DSR
 */
export async function initDSRExceptionChecks(id: string): Promise<any[]> {
  const res = await fetch(`/api/sdk/v1/compliance/dsr/${id}/exception-checks/init`, {
    method: 'POST',
    headers: getSdkHeaders(),
  })
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  return res.json()
}

/**
 * Update a single exception check
 */
export async function updateDSRExceptionCheck(dsrId: string, checkId: string, data: { applies: boolean; notes?: string }): Promise<any> {
  const res = await fetch(`/api/sdk/v1/compliance/dsr/${dsrId}/exception-checks/${checkId}`, {
    method: 'PUT',
    headers: getSdkHeaders(),
    body: JSON.stringify(data),
  })
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  return res.json()
}

// =============================================================================
// HISTORY
// =============================================================================

/**
 * Fetch status change history for a DSR
 */
export async function fetchDSRHistory(id: string): Promise<any[]> {
  const res = await fetch(`/api/sdk/v1/compliance/dsr/${id}/history`, {
    headers: getSdkHeaders(),
  })
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  return res.json()
}
