import api from './api'
import type { DisclosureRequest, DisclosureCountResponse } from '@/types'

export async function requestDisclosure(caseId: number, reason: string): Promise<DisclosureRequest> {
  const res = await api.post<DisclosureRequest>(`/cases/${caseId}/disclosure-request`, { reason })
  return res.data
}

export async function getDisclosureRequests(status?: string): Promise<DisclosureRequest[]> {
  const params = status ? { status } : {}
  const res = await api.get<DisclosureRequest[]>('/admin/disclosure-requests', { params })
  return res.data
}

export async function getDisclosureCount(): Promise<number> {
  const res = await api.get<DisclosureCountResponse>('/admin/disclosure-requests/count')
  return res.data.pending_count
}

export async function reviewDisclosure(requestId: number, status: 'approved' | 'rejected'): Promise<DisclosureRequest> {
  const res = await api.put<DisclosureRequest>(`/admin/disclosure-requests/${requestId}`, { status })
  return res.data
}

export async function getMyDisclosureRequests(): Promise<DisclosureRequest[]> {
  const res = await api.get<DisclosureRequest[]>('/cases/my-disclosure-requests')
  return res.data
}

export async function revokeDisclosure(requestId: number): Promise<DisclosureRequest> {
  const res = await api.put<DisclosureRequest>(`/cases/disclosure-requests/${requestId}/revoke`)
  return res.data
}

export async function adminRevokeDisclosure(requestId: number): Promise<DisclosureRequest> {
  const res = await api.put<DisclosureRequest>(`/admin/disclosure-requests/${requestId}/revoke`)
  return res.data
}

export async function deleteDisclosure(requestId: number): Promise<void> {
  await api.delete(`/cases/disclosure-requests/${requestId}`)
}

export async function adminDeleteDisclosure(requestId: number): Promise<void> {
  await api.delete(`/admin/disclosure-requests/${requestId}`)
}

export async function adminReactivateDisclosure(requestId: number): Promise<DisclosureRequest> {
  const res = await api.put<DisclosureRequest>(`/admin/disclosure-requests/${requestId}/reactivate`)
  return res.data
}
