/**
 * DSFA API functions and mock data.
 */

import {
  DSFASource,
  DSFACorpusStats,
} from '@/lib/sdk/types'

export const API_BASE = process.env.NEXT_PUBLIC_KLAUSUR_SERVICE_URL || 'http://localhost:8086'

export const MOCK_SOURCES: DSFASource[] = [
  {
    id: '1',
    sourceCode: 'WP248',
    name: 'WP248 rev.01 - Leitlinien zur DSFA',
    fullName: 'Leitlinien zur Datenschutz-Folgenabschaetzung',
    organization: 'Artikel-29-Datenschutzgruppe / EDPB',
    sourceUrl: 'https://ec.europa.eu/newsroom/article29/items/611236/en',
    licenseCode: 'EDPB-LICENSE',
    licenseName: 'EDPB Document License',
    attributionRequired: true,
    attributionText: 'Quelle: WP248 rev.01, Artikel-29-Datenschutzgruppe (2017)',
    documentType: 'guideline',
    language: 'de',
  },
  {
    id: '2',
    sourceCode: 'DSK_KP5',
    name: 'Kurzpapier Nr. 5 - DSFA nach Art. 35 DS-GVO',
    organization: 'Datenschutzkonferenz (DSK)',
    sourceUrl: 'https://www.datenschutzkonferenz-online.de/media/kp/dsk_kpnr_5.pdf',
    licenseCode: 'DL-DE-BY-2.0',
    licenseName: 'Datenlizenz DE \u2013 Namensnennung 2.0',
    licenseUrl: 'https://www.govdata.de/dl-de/by-2-0',
    attributionRequired: true,
    attributionText: 'Quelle: DSK Kurzpapier Nr. 5 (Stand: 2018)',
    documentType: 'guideline',
    language: 'de',
  },
  {
    id: '3',
    sourceCode: 'BFDI_MUSS_PUBLIC',
    name: 'BfDI DSFA-Liste (oeffentlicher Bereich)',
    organization: 'BfDI',
    sourceUrl: 'https://www.bfdi.bund.de',
    licenseCode: 'DL-DE-ZERO-2.0',
    licenseName: 'Datenlizenz DE \u2013 Zero 2.0',
    attributionRequired: false,
    attributionText: 'Quelle: BfDI, Liste gem. Art. 35 Abs. 4 DSGVO',
    documentType: 'checklist',
    language: 'de',
  },
  {
    id: '4',
    sourceCode: 'NI_MUSS_PRIVATE',
    name: 'LfD NI DSFA-Liste (nicht-oeffentlich)',
    organization: 'LfD Niedersachsen',
    sourceUrl: 'https://www.lfd.niedersachsen.de/download/131098',
    licenseCode: 'DL-DE-BY-2.0',
    licenseName: 'Datenlizenz DE \u2013 Namensnennung 2.0',
    attributionRequired: true,
    attributionText: 'Quelle: LfD Niedersachsen, DSFA-Muss-Liste',
    documentType: 'checklist',
    language: 'de',
  },
]

export const MOCK_STATS: DSFACorpusStats = {
  sources: [
    {
      sourceId: '1',
      sourceCode: 'WP248',
      name: 'WP248 rev.01',
      organization: 'EDPB',
      licenseCode: 'EDPB-LICENSE',
      documentType: 'guideline',
      documentCount: 1,
      chunkCount: 50,
      lastIndexedAt: '2026-02-09T10:00:00Z',
    },
    {
      sourceId: '2',
      sourceCode: 'DSK_KP5',
      name: 'DSK Kurzpapier Nr. 5',
      organization: 'DSK',
      licenseCode: 'DL-DE-BY-2.0',
      documentType: 'guideline',
      documentCount: 1,
      chunkCount: 35,
      lastIndexedAt: '2026-02-09T10:00:00Z',
    },
  ],
  totalSources: 45,
  totalDocuments: 45,
  totalChunks: 850,
  qdrantCollection: 'bp_dsfa_corpus',
  qdrantPointsCount: 850,
  qdrantStatus: 'green',
}

export async function fetchSources(): Promise<DSFASource[]> {
  try {
    const response = await fetch(`${API_BASE}/api/v1/dsfa-rag/sources`)
    if (!response.ok) throw new Error('Failed to fetch sources')
    return await response.json()
  } catch {
    return MOCK_SOURCES
  }
}

export async function fetchStats(): Promise<DSFACorpusStats> {
  try {
    const response = await fetch(`${API_BASE}/api/v1/dsfa-rag/stats`)
    if (!response.ok) throw new Error('Failed to fetch stats')
    return await response.json()
  } catch {
    return MOCK_STATS
  }
}

export async function initializeCorpus(): Promise<{ sources_registered: number }> {
  const response = await fetch(`${API_BASE}/api/v1/dsfa-rag/init`, {
    method: 'POST',
  })
  if (!response.ok) throw new Error('Failed to initialize corpus')
  return await response.json()
}

export async function triggerIngestion(sourceCode: string): Promise<void> {
  const response = await fetch(`${API_BASE}/api/v1/dsfa-rag/sources/${sourceCode}/ingest`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({}),
  })
  if (!response.ok) throw new Error('Failed to trigger ingestion')
}
