import { Router } from 'express';
import { AdminController } from '../controllers/admin.controller';
import { validateRequest } from '../middleware/validation.middleware';
import { apiKeyMiddleware } from '../middleware/api-key.middleware';
import {
  createNetworkSchema,
  createTokenSchema,
  updateTokenSchema,
  updateTokenMinimumSwapValueSchema,
  tokenIdParamSchema,
  fetchTokenDataSchema,
} from '../../application/dto';

/**
 * Admin routes mounted under `/api/v1`.
 *
 * All routes require the `x-api-key` header.
 *
 * Routes:
 *  - POST /admin/networks   Create a network.
 *                           Header: x-api-key
 *                           Body: { name, code, addressType, thumbnail?, explorer, chainId?, status?, mcap? }
 *  - POST /admin/tokens     Create a token.
 *                           Header: x-api-key
 *                           Body: { isNative, address?, networkId, thumbnail?, name, symbol, decimals, code, info?, isActive?, minimumSwapValue? }
 *  - PATCH /admin/tokens/:id  Update a token.
 *                           Header: x-api-key
 *                           Body: { isNative?, address?, networkId?, thumbnail?, name?, symbol?, decimals?, code?, cmcId?, info?, isActive?, minimumSwapValue? }
 *  - DELETE /admin/tokens/:id  Delete a token.
 *                           Header: x-api-key
 *  - PATCH /admin/tokens/:id/minimum-swap-value  Set a custom minimum swap value for a token.
 *                           Header: x-api-key
 *                           Body: { minimumSwapValue: number | null }
 */
export const createAdminRoutes = (controller: AdminController): Router => {
  const router = Router();

  router.post(
    '/admin/networks',
    apiKeyMiddleware,
    validateRequest({ body: createNetworkSchema }),
    controller.createNetwork,
  );

  router.post(
    '/admin/tokens',
    apiKeyMiddleware,
    validateRequest({ body: createTokenSchema }),
    controller.createToken,
  );

  router.patch(
    '/admin/tokens/:id',
    apiKeyMiddleware,
    validateRequest({
      params: tokenIdParamSchema,
      body: updateTokenSchema,
    }),
    controller.updateToken,
  );

  router.delete(
    '/admin/tokens/:id',
    apiKeyMiddleware,
    validateRequest({
      params: tokenIdParamSchema,
    }),
    controller.deleteToken,
  );

  router.patch(
    '/admin/tokens/:id/minimum-swap-value',
    apiKeyMiddleware,
    validateRequest({
      params: tokenIdParamSchema,
      body: updateTokenMinimumSwapValueSchema,
    }),
    controller.updateTokenMinimumSwapValue,
  );

  router.post(
    '/admin/tokens/fetch',
    apiKeyMiddleware,
    validateRequest({ body: fetchTokenDataSchema }),
    controller.fetchTokenData,
  );

  return router;
};
