export default defineEventHandler(async (event) => {
  const body = await readBody(event)

  // Mock data lengkap
  const mockData = [
    {
      id: 'RSC001',
      resource_type: 'Encounter',
      patient: {
        name: 'Ahmad Wepe',
        mrn: 'RM001234',
      },
      status: 2, // 0: failed, 1: pending, 2: success
      updated_at: '2025-01-15',
      fhir_id: 'ENC-00123',
    },
    {
      id: 'RSC002',
      resource_type: 'Patient',
      patient: {
        name: 'Siti Aminah',
        mrn: 'RM001235',
      },
      status: 1,
      updated_at: '2025-01-10',
      fhir_id: 'PAT-001235',
    },
    {
      id: 'RSC003',
      resource_type: 'Observation',
      patient: {
        name: 'Budi Antono',
        mrn: 'RM001236',
      },
      status: 0,
      updated_at: '2025-01-11',
      fhir_id: 'OBS-001236',
    },
    {
      id: 'RSC004',
      resource_type: 'Encounter',
      patient: {
        name: 'Maria Sari',
        mrn: 'RM001237',
      },
      status: 2,
      updated_at: '2025-01-12',
      fhir_id: 'ENC-001237',
    },
    {
      id: 'RSC005',
      resource_type: 'Patient',
      patient: {
        name: 'Joko Widodo',
        mrn: 'RM001238',
      },
      status: 1,
      updated_at: '2025-01-13',
      fhir_id: 'PAT-001238',
    },
    {
      id: 'RSC006',
      resource_type: 'Observation',
      patient: {
        name: 'Dewi Sartika',
        mrn: 'RM001239',
      },
      status: 2,
      updated_at: '2025-01-14',
      fhir_id: 'OBS-001239',
    },
    {
      id: 'RSC007',
      resource_type: 'Encounter',
      patient: {
        name: 'Rudi Hartono',
        mrn: 'RM001240',
      },
      status: 0,
      updated_at: '2025-01-16',
      fhir_id: 'ENC-001240',
    },
    {
      id: 'RSC008',
      resource_type: 'Patient',
      patient: {
        name: 'Sri Mulyani',
        mrn: 'RM001241',
      },
      status: 2,
      updated_at: '2025-01-17',
      fhir_id: 'PAT-001241',
    },
  ]

  // Ekstrak parameter filter dari request body
  const {
    status,
    resource_type,
    date_from,
    date_to,
    search,
    page = 1,
    limit = 10,
  } = body

  let filteredData = [...mockData]

  // Filter berdasarkan status
  if (status !== undefined && status !== null && status !== '') {
    filteredData = filteredData.filter(item => item.status === Number(status))
  }

  // Filter berdasarkan resource_type (transaction type)
  if (resource_type && resource_type !== 'all') {
    filteredData = filteredData.filter(item =>
      item.resource_type.toLowerCase() === resource_type.toLowerCase(),
    )
  }

  // Filter berdasarkan rentang tanggal
  if (date_from) {
    filteredData = filteredData.filter(item =>
      new Date(item.updated_at) >= new Date(date_from),
    )
  }

  if (date_to) {
    filteredData = filteredData.filter(item =>
      new Date(item.updated_at) <= new Date(date_to),
    )
  }

  // Filter berdasarkan pencarian nama pasien atau MRN
  if (search) {
    filteredData = filteredData.filter(item =>
      item.patient.name.toLowerCase().includes(search.toLowerCase())
      || item.patient.mrn.toLowerCase().includes(search.toLowerCase())
      || item.fhir_id.toLowerCase().includes(search.toLowerCase()),
    )
  }

  // Pagination
  const totalItems = filteredData.length
  const totalPages = Math.ceil(totalItems / limit)
  const offset = (page - 1) * limit
  const paginatedData = filteredData.slice(offset, offset + limit)

  // Simulasi delay untuk loading state
  await new Promise(resolve => setTimeout(resolve, 300))

  return {
    success: true,
    data: paginatedData,
    meta: {
      total: totalItems,
      page: Number(page),
      limit: Number(limit),
      total_pages: totalPages,
      has_next: page < totalPages,
      has_prev: page > 1,
    },
    filters: {
      status,
      resource_type,
      date_from,
      date_to,
      search,
    },
  }
})
