import { apiRequest } from './api'; export type InspectionReportStatus = 'WORKING' | 'OFFICIALIZED' | 'FROZEN' | 'CANCELLED'; export type InspectionReportFollowUpType = | 'COMPANY_NOTE' | 'COMPANY_DOCUMENT' | 'INTERNAL_NOTE' | 'VERIFICATION' | 'OTHER'; export interface PageMetaF4 { page: number; pageSize: number; total: number; totalPages: number; } export interface InspectionReportQueryF4 { page?: number; pageSize?: number; search?: string; year?: number | string; companyId?: string; areaId?: string; inspectorId?: string; dateFrom?: string; dateTo?: string; } export interface InspectionReportDetailF4 { id: string; visitId: string; actId: string; reportYear: number; reportNumber: number; code: string; status: InspectionReportStatus; executiveSummary: string | null; reportDescription: string | null; gedoIfIdentifier: string | null; gedoOfficializedAt: string | null; gedoPdfOriginalName: string | null; gedoPdfSha256: string | null; pdfStatus: 'PENDING' | 'READY' | 'FAILED'; wordStatus: 'PENDING' | 'READY' | 'FAILED'; wordGeneratedAt: string | null; title: string; actVersion: number; actClosureSha256: string; frozenSha256: string; generatedAt: string; generatedBy: { id: string; username: string; firstName: string; lastName: string }; act: { id: string; code: string; title: string; status: string; occurredAt: string; sealedAt: string | null; urgency: string; deadlineAt: string | null; }; visit: { id: string; code: string; status: string }; companies: Array<{ id: string; code: string; name: string }>; areas: Array<{ id: string; code: string; name: string }>; findingCount: number; responseDueOn: string | null; deadlineReason: string | null; deadlines: Array<{ id: string; reportId: string | null; responseDueOn: string; reason: string; createdAt: string }>; findings: Array<{ id: string; code: string; title: string; status: string; assetCode: string; assetName: string; responseDueOn: string | null }>; companyResponses: Array<{ id: string; reportId: string | null; receivedOn: string; details: string | null; committedCorrectionOn: string | null; contactName: string | null; contactEmail: string | null; originalName: string | null; sizeBytes: number | null; sha256: string | null; createdAt: string; }>; } export interface PendingInspectionReportF4 { actId: string; visitId: string; actCode: string; actTitle: string; actYear: number; occurredAt: string; sealedAt: string; closureSha256: string; visitCode: string; companies: Array<{ id: string; code: string; name: string }>; areas: Array<{ id: string; code: string; name: string }>; findingCount: number; } export interface InspectionReportFollowUp { id: string; reportId: string; type: InspectionReportFollowUpType; externalReference: string | null; occurredAt: string; description: string | null; originalName: string | null; mimeType: string | null; sizeBytes: number | null; sha256: string | null; createdBy: string | null; createdAt: string; } export interface InspectionReportWorkflowView { id: string; actId: string; visitId: string; code: string; status: InspectionReportStatus; executiveSummary: string | null; reportDescription: string | null; gedoIfIdentifier: string | null; gedoOfficializedAt: string | null; act: { code: string; urgency: string; deadlineDays: number | null; deadlineDayType: 'BUSINESS' | 'CALENDAR' | null; deadlineBasis: string | null; deadlineBaseAt: string | null; deadlineAt: string | null; } | null; followUps: InspectionReportFollowUp[]; } function queryString(query: InspectionReportQueryF4): string { const params = new URLSearchParams(); const add = (key: string, value: string | number | undefined) => { if (value === undefined || value === '') return; params.set(key, String(value)); }; add('page', query.page ?? 1); add('pageSize', query.pageSize ?? 25); add('search', query.search?.trim() || undefined); add('year', query.year); add('companyId', query.companyId); add('areaId', query.areaId); add('inspectorId', query.inspectorId); add('dateFrom', query.dateFrom); add('dateTo', query.dateTo); return params.toString(); } export function listInspectionReportsF4(query: InspectionReportQueryF4) { return apiRequest<{ data: InspectionReportDetailF4[]; meta: PageMetaF4 }>(`/inspection-reports?${queryString(query)}`); } export function listPendingInspectionReportsF4(query: InspectionReportQueryF4) { return apiRequest<{ data: PendingInspectionReportF4[]; meta: PageMetaF4 }>(`/inspection-reports/pending?${queryString(query)}`); } export function getInspectionReportF4(id: string) { return apiRequest(`/inspection-reports/${id}`); } export function listInspectionReportFollowUps(id: string) { return apiRequest(`/inspection-reports/${id}/follow-ups`); } export function updateInspectionReportNarrative( id: string, input: { executiveSummary?: string | null; description?: string | null }, ) { return apiRequest(`/inspection-reports/${id}`, { method: 'PATCH', body: JSON.stringify(input), }); } export function officializeInspectionReport( id: string, input: { gedoIfIdentifier: string; gedoOfficializedAt: string; file: File }, ) { const body = new FormData(); body.set('gedoIfIdentifier', input.gedoIfIdentifier); body.set('gedoOfficializedAt', input.gedoOfficializedAt); body.set('file', input.file); return apiRequest(`/inspection-reports/${id}/officialize`, { method: 'POST', body, }); } export function setInspectionReportResponseDeadline( id: string, input: { responseDueOn: string; reason?: string | null }, ) { return apiRequest(`/inspection-reports/${id}/response-deadline`, { method: 'PATCH', body: JSON.stringify(input), }); } export function addInspectionReportCompanyResponse( id: string, input: { receivedOn: string; details?: string; committedCorrectionOn?: string; contactName?: string; contactEmail?: string; file?: File | null }, ) { const body = new FormData(); body.set('receivedOn', input.receivedOn); if (input.details?.trim()) body.set('details', input.details.trim()); if (input.committedCorrectionOn) body.set('committedCorrectionOn', input.committedCorrectionOn); if (input.contactName?.trim()) body.set('contactName', input.contactName.trim()); if (input.contactEmail?.trim()) body.set('contactEmail', input.contactEmail.trim()); if (input.file) body.set('file', input.file); return apiRequest(`/inspection-reports/${id}/company-responses`, { method: 'POST', body }); } export function addInspectionReportFollowUp( id: string, input: { type: InspectionReportFollowUpType; occurredAt: string; externalReference?: string | null; description?: string | null; file?: File | null; }, ) { const body = new FormData(); body.set('type', input.type); body.set('occurredAt', input.occurredAt); if (input.externalReference?.trim()) body.set('externalReference', input.externalReference.trim()); if (input.description?.trim()) body.set('description', input.description.trim()); if (input.file) body.set('file', input.file); return apiRequest(`/inspection-reports/${id}/follow-ups`, { method: 'POST', body, }); } export function inspectionReportWordDownloadUrl(id: string) { return `/api/v3/inspection-reports/${id}/word`; } export function inspectionReportGedoPdfDownloadUrl(id: string) { return `/api/v3/inspection-reports/${id}/gedo-pdf`; } export function inspectionReportCompanyResponseDownloadUrl(reportId: string, responseId: string) { return `/api/v3/inspection-reports/${reportId}/company-responses/${responseId}/content`; } export function inspectionReportFollowUpDownloadUrl(reportId: string, followUpId: string) { return `/api/v3/inspection-reports/${reportId}/follow-ups/${followUpId}/content`; } export function inspectionReportConsolidatedWordDownloadUrl(reportId: string) { return `/api/v3/inspection-reports/${reportId}/consolidated-word`; }