import { useState } from 'react' import type { DatasetCreateResponse, JobRead, QaComparisonRequest, QaComparisonResult, QualityCheckRead } from '../types' import { formatError } from '../lib/formatError' import { qaApi } from '../services/api' interface UseMapSelectionQaOptions { selectedProjectId: string | null latestSelectionDataset: DatasetCreateResponse | null loadQualityChecks: (projectId?: string | null) => Promise loadProjectData: (projectId: string) => Promise } export function useMapSelectionQa({ selectedProjectId, latestSelectionDataset, loadQualityChecks, loadProjectData, }: UseMapSelectionQaOptions) { const [selectedMapQaReferenceDatasetId, setSelectedMapQaReferenceDatasetId] = useState('') const [mapSelectionQaRunning, setMapSelectionQaRunning] = useState(false) const [mapSelectionQaError, setMapSelectionQaError] = useState(null) const [mapSelectionQaResult, setMapQaResult] = useState(null) const [latestMapSelectionQualityCheckId, setLatestMapSelectionQualityCheckId] = useState(null) const runMapSelectionQa = async (candidateDataset = latestSelectionDataset) => { if (!selectedProjectId) { setMapSelectionQaError('Kies eerst een werkruimte om een kwaliteitscontrole te draaien.') return null } if (!candidateDataset) { setMapSelectionQaError('Save the map selection as a dataset before running QA/QC.') return null } if (!selectedMapQaReferenceDatasetId) { setMapSelectionQaError('Kies eerst een referentiebron voor de kwaliteitscontrole.') return null } if (candidateDataset.id === selectedMapQaReferenceDatasetId) { setMapSelectionQaError('Candidate and reference datasets must be different.') return null } setMapSelectionQaRunning(true) setMapSelectionQaError(null) setMapQaResult(null) setLatestMapSelectionQualityCheckId(null) try { const request: QaComparisonRequest = { candidate_dataset_id: candidateDataset.id, reference_dataset_id: selectedMapQaReferenceDatasetId, iou_threshold: 0.5, area_id: candidateDataset.area_id ?? null, } const job: JobRead = await qaApi.runQa(selectedProjectId, request) if (job.status === 'failed') { setMapSelectionQaError(job.error_message || 'Map selection QA/QC failed') return null } const payload = job.result_json if (!payload || typeof payload !== 'object') { setMapSelectionQaError('Map selection QA/QC result was not available') return null } const parsed = payload as unknown as QaComparisonResult if (!parsed || typeof parsed.status !== 'string') { setMapSelectionQaError('Map selection QA/QC result format was unexpected') return null } setMapQaResult(parsed) setLatestMapSelectionQualityCheckId(parsed.quality_check_id ?? null) await loadQualityChecks(selectedProjectId) await loadProjectData(selectedProjectId) return parsed } catch (error) { setMapSelectionQaError(formatError(error, 'Map selection QA/QC failed')) return null } finally { setMapSelectionQaRunning(false) } } return { selectedMapQaReferenceDatasetId, mapSelectionQaRunning, mapSelectionQaError, mapSelectionQaResult, latestMapSelectionQualityCheckId, runMapSelectionQa, setSelectedMapQaReferenceDatasetId, } }