import { http, HttpResponse } from 'msw'
import {
  mockTokenResponse,
  mockUser,
  mockCase,
  mockCaseNoIcd,
  mockCaseListResponse,
  mockDashboardResponse,
  mockNotificationList,
  mockReportMeta,
  mockUserResponse,
  mockAuditLogEntry,
  mockDisclosureRequest,
  mockDisclosureApproved,
} from './data'

export const handlers = [
  // ==================== Auth ====================

  // POST /api/auth/login
  http.post('/api/auth/login', () => {
    return HttpResponse.json(mockTokenResponse)
  }),

  // POST /api/auth/register
  http.post('/api/auth/register', () => {
    return HttpResponse.json({ user: mockUser })
  }),

  // POST /api/auth/logout
  http.post('/api/auth/logout', () => {
    return new HttpResponse(null, { status: 204 })
  }),

  // GET /api/auth/me
  http.get('/api/auth/me', () => {
    return HttpResponse.json(mockUser)
  }),

  // POST /api/auth/refresh
  http.post('/api/auth/refresh', () => {
    return HttpResponse.json({
      access_token: 'refreshed-access-token',
      refresh_token: 'refreshed-refresh-token',
      token_type: 'bearer',
    })
  }),

  // PUT /api/auth/profile
  http.put('/api/auth/profile', () => {
    return HttpResponse.json(mockUser)
  }),

  // POST /api/auth/avatar
  http.post('/api/auth/avatar', () => {
    return HttpResponse.json({ ...mockUser, avatar_url: '/uploads/avatar-1.png' })
  }),

  // DELETE /api/auth/avatar
  http.delete('/api/auth/avatar', () => {
    return HttpResponse.json({ ...mockUser, avatar_url: null })
  }),

  // POST /api/auth/change-password
  http.post('/api/auth/change-password', () => {
    return new HttpResponse(null, { status: 200 })
  }),

  // POST /api/auth/mfa/setup
  http.post('/api/auth/mfa/setup', () => {
    return HttpResponse.json({
      secret: 'JBSWY3DPEHPK3PXP',
      qr_uri: 'otpauth://totp/DAK:admin@dak.de?secret=JBSWY3DPEHPK3PXP&issuer=DAK',
    })
  }),

  // POST /api/auth/mfa/verify
  http.post('/api/auth/mfa/verify', () => {
    return new HttpResponse(null, { status: 200 })
  }),

  // DELETE /api/auth/mfa
  http.delete('/api/auth/mfa', () => {
    return new HttpResponse(null, { status: 200 })
  }),

  // ==================== Cases ====================

  // GET /api/cases/
  http.get('/api/cases/', () => {
    return HttpResponse.json(mockCaseListResponse)
  }),

  // GET /api/cases/pending-icd
  http.get('/api/cases/pending-icd', () => {
    return HttpResponse.json({
      items: [mockCaseNoIcd],
      total: 1,
      page: 1,
      per_page: 20,
    })
  }),

  // PUT /api/cases/:id
  http.put('/api/cases/:id', ({ params }) => {
    const id = Number(params.id)
    return HttpResponse.json({ ...mockCase, id })
  }),

  // PUT /api/cases/:id/kvnr
  http.put('/api/cases/:id/kvnr', ({ params }) => {
    const id = Number(params.id)
    return HttpResponse.json({ ...mockCase, id, kvnr: 'C111222333' })
  }),

  // PUT /api/cases/:id/icd
  http.put('/api/cases/:id/icd', ({ params }) => {
    const id = Number(params.id)
    return HttpResponse.json({ ...mockCase, id, icd: 'C61' })
  }),

  // POST /api/cases/:caseId/disclosure-request
  http.post('/api/cases/:caseId/disclosure-request', () => {
    return HttpResponse.json(mockDisclosureRequest)
  }),

  // ==================== Dashboard ====================

  // GET /api/reports/dashboard
  http.get('/api/reports/dashboard', () => {
    return HttpResponse.json(mockDashboardResponse)
  }),

  // ==================== Reports ====================

  // GET /api/reports/list
  http.get('/api/reports/list', () => {
    return HttpResponse.json({
      items: [mockReportMeta],
      total: 1,
    })
  }),

  // POST /api/reports/generate
  http.post('/api/reports/generate', () => {
    return HttpResponse.json(mockReportMeta)
  }),

  // DELETE /api/reports/delete
  http.delete('/api/reports/delete', () => {
    return HttpResponse.json({ deleted: 1 })
  }),

  // ==================== Notifications ====================

  // GET /api/notifications
  http.get('/api/notifications', () => {
    return HttpResponse.json(mockNotificationList)
  }),

  // PUT /api/notifications/:id/read
  http.put('/api/notifications/:id/read', ({ params }) => {
    const id = Number(params.id)
    return HttpResponse.json({
      ...mockNotificationList.items[0],
      id,
      is_read: true,
    })
  }),

  // PUT /api/notifications/read-all
  http.put('/api/notifications/read-all', () => {
    return HttpResponse.json({ marked_read: 1 })
  }),

  // ==================== Admin: Users ====================

  // GET /api/admin/users
  http.get('/api/admin/users', () => {
    return HttpResponse.json([mockUserResponse])
  }),

  // POST /api/admin/users
  http.post('/api/admin/users', () => {
    return HttpResponse.json(mockUserResponse)
  }),

  // PUT /api/admin/users/:id
  http.put('/api/admin/users/:id', () => {
    return HttpResponse.json(mockUserResponse)
  }),

  // ==================== Admin: Audit Log ====================

  // GET /api/admin/audit-log
  http.get('/api/admin/audit-log', () => {
    return HttpResponse.json([mockAuditLogEntry])
  }),

  // ==================== Admin: Disclosure Requests ====================

  // GET /api/admin/disclosure-requests
  http.get('/api/admin/disclosure-requests', () => {
    return HttpResponse.json([mockDisclosureRequest])
  }),

  // GET /api/admin/disclosure-requests/count
  http.get('/api/admin/disclosure-requests/count', () => {
    return HttpResponse.json({ pending_count: 1 })
  }),

  // PUT /api/admin/disclosure-requests/:id
  http.put('/api/admin/disclosure-requests/:id', () => {
    return HttpResponse.json(mockDisclosureApproved)
  }),
]
