import { ipcMain } from 'electron'

import {
  deleteAllReports,
  deleteReport,
  downloadClassReports,
  downloadReport,
  generateReports,
  loadReports,
  openReport
} from '../services/reportService'
import type {
  DeleteAllReportsResponse,
  DeleteReportResponse,
  DownloadClassReportsInput,
  DownloadClassReportsResponse,
  DownloadReportResponse,
  GenerateReportsInput,
  GenerateReportsResponse,
  LoadReportsResponse,
  OpenReportResponse
} from '../types/report'

function getErrorMessage(error: unknown): string {
  return error instanceof Error ? error.message : '报告操作失败'
}

export function registerReportIpc(): void {
  ipcMain.handle('reports:load', async (): Promise<LoadReportsResponse> => {
    try {
      return {
        ok: true,
        reports: await loadReports()
      }
    } catch (error) {
      return {
        ok: false,
        message: getErrorMessage(error)
      }
    }
  })

  ipcMain.handle(
    'reports:generate',
    async (_, payload: GenerateReportsInput): Promise<GenerateReportsResponse> => {
      try {
        return {
          ok: true,
          ...(await generateReports(payload))
        }
      } catch (error) {
        return {
          ok: false,
          message: getErrorMessage(error)
        }
      }
    }
  )

  ipcMain.handle('reports:open', async (_, filePath: string): Promise<OpenReportResponse> => {
    try {
      await openReport(filePath)
      return {
        ok: true
      }
    } catch (error) {
      return {
        ok: false,
        message: getErrorMessage(error)
      }
    }
  })

  ipcMain.handle('reports:download', async (_, id: string): Promise<DownloadReportResponse> => {
    try {
      const result = await downloadReport(id)

      if (result.canceled) {
        return {
          ok: false,
          canceled: true,
          message: '已取消下载'
        }
      }

      return {
        ok: true,
        filePath: result.filePath
      }
    } catch (error) {
      return {
        ok: false,
        message: getErrorMessage(error)
      }
    }
  })

  ipcMain.handle(
    'reports:download-class',
    async (_, payload: DownloadClassReportsInput): Promise<DownloadClassReportsResponse> => {
      try {
        const result = await downloadClassReports(payload)

        if (result.canceled) {
          return {
            ok: false,
            canceled: true,
            message: '已取消下载'
          }
        }

        return {
          ok: true,
          filePath: result.filePath,
          reportCount: result.reportCount
        }
      } catch (error) {
        return {
          ok: false,
          message: getErrorMessage(error)
        }
      }
    }
  )

  ipcMain.handle('reports:delete', async (_, id: string): Promise<DeleteReportResponse> => {
    try {
      await deleteReport(id)
      return {
        ok: true
      }
    } catch (error) {
      return {
        ok: false,
        message: getErrorMessage(error)
      }
    }
  })

  ipcMain.handle('reports:delete-all', async (): Promise<DeleteAllReportsResponse> => {
    try {
      return {
        ok: true,
        deletedCount: await deleteAllReports()
      }
    } catch (error) {
      return {
        ok: false,
        message: getErrorMessage(error)
      }
    }
  })
}
