import express from 'express';
import { wrapAsync } from '@/api/http/request';
import permissions from '@/middlewares/check-permissions';
import { FEATURE_PERMISSIONS } from '@/shared/constants/product-permissions';
import * as zone_checkins from '@/api/controllers/zone_checkins.controller';

const router = express.Router();

const canCheckIn = permissions.checkPermissions(FEATURE_PERMISSIONS.ZONE_CHECKIN);

/**
 * @openapi
 * /api/zone_checkins/today:
 *   get:
 *     tags: [Zone Check-in]
 *     summary: Today's zone check-in for the caller (campus-local date)
 *     description: >
 *       Requires explicit ZONE_CHECKIN. "Today" is the
 *       caller's campus-local date (campus `timezone`), computed server-side.
 *     responses:
 *       200:
 *         description: '{ date, zone, isCheckedInToday }'
 *       401: { $ref: '#/components/responses/UnauthorizedError' }
 *       403: { $ref: '#/components/responses/ForbiddenError' }
 * /api/zone_checkins:
 *   get:
 *     tags: [Zone Check-in]
 *     summary: The caller's check-in history
 *     description: Requires ZONE_CHECKIN. Optional `from` / `to` (YYYY-MM-DD) range.
 *     responses:
 *       200: { description: '{ rows: [{ date, zone }], count }' }
 *       403: { $ref: '#/components/responses/ForbiddenError' }
 *   post:
 *     tags: [Zone Check-in]
 *     summary: Record today's zone (upsert)
 *     description: "Requires ZONE_CHECKIN. Body { data: { zone } } (blue|green|yellow|red)."
 *     responses:
 *       200: { description: '{ date, zone, isCheckedInToday }' }
 *       400: { $ref: '#/components/responses/ValidationError' }
 *       403: { $ref: '#/components/responses/ForbiddenError' }
 */
router.get('/today', canCheckIn, wrapAsync(zone_checkins.today));
router.get('/', canCheckIn, wrapAsync(zone_checkins.history));
router.post('/', canCheckIn, wrapAsync(zone_checkins.checkIn));
router.delete('/today', canCheckIn, wrapAsync(zone_checkins.clearToday));

export default router;
