// Public · Pages — the block-based CMS pages, read side. Counterpart of
// admin/pages.router.js (the page builder). Unrelated to /admin/shard/pages,
// which is the in-game help-page queue.
//
// Mounted at /api/v1/public/pages by public/index.js. No group gate.
//
// Declaration order is load-bearing: the draft-preview route is registered ahead
// of /:slug, and it is deliberately NOT site-mode gated so a preview link keeps
// working during maintenance — the single-use token is the access control.

const express = require('express')

const ctrl = require('./public.controller')
const siteMode = require('../../../middleware/siteMode')

const pagesRouter = express.Router()

pagesRouter.get(
  '/:id/preview/:token',
  // #swagger.tags = ['Public']
  // #swagger.summary = 'Render a page from a draft-preview token'
  // #swagger.parameters['id'] = { in: 'path', required: true, schema: { type: 'integer' }, description: 'Page id.' }
  // #swagger.parameters['token'] = { in: 'path', required: true, schema: { type: 'string' }, description: 'Preview token from POST /admin/pages/:id/preview.' }
  /* #swagger.responses[200] = { description: 'The page (any status)', content: { "application/json": { schema: { type: "object", additionalProperties: true } } } } */
  /* #swagger.responses[404] = { description: 'Token invalid/expired or page missing', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
  ctrl.getPagePreview,
)
pagesRouter.get(
  '/:slug',
  // #swagger.tags = ['Public']
  // #swagger.summary = 'Get a published CMS page by slug'
  // #swagger.description = 'Drafts 404 for the public; staff sessions see drafts. Gated by site mode.'
  // #swagger.parameters['slug'] = { in: 'path', required: true, schema: { type: 'string' }, description: 'Page slug.' }
  /* #swagger.responses[200] = { description: 'The page', content: { "application/json": { schema: { type: "object", additionalProperties: true } } } } */
  /* #swagger.responses[404] = { description: 'Not found', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
  siteMode,
  ctrl.getPage,
)

module.exports = pagesRouter
