/**
 * GET /api/v1/admin/audit-logs/[id]
 *
 * Get a single audit log entry by ID.
 *
 * Response: AdminSingleResponse<AdminAuditLog>
 */

import { db } from '@sim/db'
import { auditLog } from '@sim/db/schema'
import { createLogger } from '@sim/logger'
import { eq } from 'drizzle-orm'
import { v1AdminGetAuditLogContract } from '@/lib/api/contracts/v1/audit-logs'
import { parseRequest } from '@/lib/api/server'
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
import { withAdminAuthParams } from '@/app/api/v1/admin/middleware'
import {
  adminValidationErrorResponse,
  internalErrorResponse,
  notFoundResponse,
  singleResponse,
} from '@/app/api/v1/admin/responses'
import { toAdminAuditLog } from '@/app/api/v1/admin/types'

const logger = createLogger('AdminAuditLogDetailAPI')

interface RouteParams {
  id: string
}

export const GET = withRouteHandler(
  withAdminAuthParams<RouteParams>(async (request, context) => {
    const parsed = await parseRequest(v1AdminGetAuditLogContract, request, context, {
      validationErrorResponse: adminValidationErrorResponse,
    })
    if (!parsed.success) return parsed.response

    const { id } = parsed.data.params

    try {
      const [log] = await db.select().from(auditLog).where(eq(auditLog.id, id)).limit(1)

      if (!log) {
        return notFoundResponse('AuditLog')
      }

      logger.info(`Admin API: Retrieved audit log ${id}`)

      return singleResponse(toAdminAuditLog(log))
    } catch (error) {
      logger.error('Admin API: Failed to get audit log', { error, id })
      return internalErrorResponse('Failed to get audit log')
    }
  })
)
