// Admin · Discord Bot — control plane for the bot process.
//
// Mounted at /api/v1/admin/discord-bot by admin/index.js, which already applied
// `noindex, isLoggedIn, staffOnly`. The bot token is entered and enabled here,
// never through an env var, and is write-only over this API (SECURITY note in
// discordBot.controller.js).
//
// Admin-only, and kept as a per-route gate rather than a router-level `use` so
// the middleware chain each route carries is unchanged by the move.

const express = require('express')
const { body } = require('express-validator')

const discordBot = require('./discordBot.controller')
const { requireRole } = require('../../../utils/auth')
const validate = require('../../../middleware/validate')

const discordBotRouter = express.Router()
const adminOnly = requireRole('admin')

discordBotRouter.get(
  '/config',
  // #swagger.tags = ['Admin · Discord Bot']
  // #swagger.summary = 'Get Discord bot config + live status (admin only)'
  // #swagger.security = [{ "cookieAuth": [] }, { "bearerAuth": [] }]
  /* #swagger.responses[200] = { description: 'Masked config + live status', content: { "application/json": { schema: { type: "object", additionalProperties: true } } } } */
  /* #swagger.responses[401] = { description: 'Not authenticated', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
  /* #swagger.responses[403] = { description: 'Admin role required', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
  adminOnly,
  discordBot.getConfig,
)
discordBotRouter.put(
  '/config',
  // #swagger.tags = ['Admin · Discord Bot']
  // #swagger.summary = 'Save Discord bot config (admin only)'
  // #swagger.description = 'token is write-only — omit/blank it to keep the existing one unchanged.'
  // #swagger.security = [{ "cookieAuth": [] }, { "bearerAuth": [] }]
  /* #swagger.requestBody = { required: true, content: { "application/json": { schema: { type: "object", properties: { guildId: { type: "string" }, token: { type: "string" }, enabled: { type: "boolean" } } } } } } */
  /* #swagger.responses[200] = { description: 'Updated config + live status', content: { "application/json": { schema: { type: "object", additionalProperties: true } } } } */
  /* #swagger.responses[400] = { description: 'Validation error, invalid token, or missing token while enabling', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
  /* #swagger.responses[401] = { description: 'Not authenticated', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
  /* #swagger.responses[403] = { description: 'Admin role required', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
  adminOnly,
  body('guildId').optional({ values: 'falsy' }).isString().trim(),
  body('token').optional({ values: 'falsy' }).isString().trim(),
  body('enabled').optional().isBoolean(),
  validate,
  discordBot.saveConfig,
)

module.exports = discordBotRouter
