import { Router } from 'express'

import {
  createAdminInventoryItem,
  importAdminInventoryItems,
  invalidateAdminInventoryItem,
  releaseAdminInventoryItem,
} from '../../services/admin/admin-write-service.js'
import {
  getAdminInventoryItems,
  getAdminInventorySkuSuggestions,
} from '../../services/admin/admin-read-service.js'
import { createJsonHandler, requireAdminRoles } from './shared.js'
import type {
  AdminInventoryCreateRouteBody,
  AdminInventoryImportRouteBody,
  AdminInventoryInvalidateRouteBody,
  AdminInventoryRouteParams,
  AdminInventoryRouteQuery,
  AdminInventorySkuSuggestionRouteQuery,
} from '../../types/admin-route-inputs.js'
import type { AdminInventoryMutationResponse } from '../../types/admin-write-models.js'

const router = Router()

router.use('/inventory', requireAdminRoles(['admin', 'operator']))

router.get('/inventory', createJsonHandler(
  (req) => getAdminInventoryItems(
    req.query as AdminInventoryRouteQuery,
    req.adminSession || null,
  ),
  {
    successMessage: 'ok',
    errorMessage: '读取库存列表失败',
    scope: '[admin/inventory]',
  },
))

router.get('/inventory/sku-suggestions', createJsonHandler(
  (req) => getAdminInventorySkuSuggestions(
    req.query as AdminInventorySkuSuggestionRouteQuery,
    req.adminSession || null,
  ),
  {
    successMessage: 'ok',
    errorMessage: '读取内部 SKU 建议失败',
    scope: '[admin/inventory/sku-suggestions]',
  },
))

router.post('/inventory', createJsonHandler(
  (req) => createAdminInventoryItem(req.body as AdminInventoryCreateRouteBody),
  {
    successMessage: '库存项已新增',
    errorMessage: '新增库存项失败',
    scope: '[admin/inventory]',
  },
))

router.post('/inventory/import', createJsonHandler(
  (req) => importAdminInventoryItems(req.body as AdminInventoryImportRouteBody),
  {
    successMessage: '导入完成',
    errorMessage: '导入库存凭据失败',
    scope: '[admin/inventory/import]',
  },
))

router.post('/inventory/:inventoryItemId/release', requireAdminRoles(['admin']), createJsonHandler(
  (req) => releaseAdminInventoryItem((req.params as AdminInventoryRouteParams).inventoryItemId),
  {
    successMessage: '库存项已释放',
    errorMessage: '释放库存项失败',
    scope: '[admin/inventory/:inventoryItemId/release]',
    audit: (req, data) => {
      const result = data as AdminInventoryMutationResponse
      return {
        action: 'inventory_item_released',
        targetType: 'inventory_item',
        targetId: String((req.params as AdminInventoryRouteParams).inventoryItemId),
        data: {
          inventoryItemId: result.inventoryItem?.inventoryItemId,
          skuCode: result.inventoryItem?.skuCode,
          displayValue: result.inventoryItem?.displayValue,
        },
      }
    },
  },
))

router.post('/inventory/:inventoryItemId/invalidate', requireAdminRoles(['admin']), createJsonHandler(
  (req) => invalidateAdminInventoryItem(
    (req.params as AdminInventoryRouteParams).inventoryItemId,
    req.body as AdminInventoryInvalidateRouteBody,
  ),
  {
    successMessage: '库存项已作废',
    errorMessage: '作废库存项失败',
    scope: '[admin/inventory/:inventoryItemId/invalidate]',
    audit: (req, data) => {
      const result = data as AdminInventoryMutationResponse
      return {
        action: 'inventory_item_invalidated',
        targetType: 'inventory_item',
        targetId: String((req.params as AdminInventoryRouteParams).inventoryItemId),
        data: {
          inventoryItemId: result.inventoryItem?.inventoryItemId,
          skuCode: result.inventoryItem?.skuCode,
          invalidReason: result.inventoryItem?.invalidReason,
        },
      }
    },
  },
))

export default router
