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 audio_files from '@/api/controllers/audio_files.controller';

const router = express.Router();

const canRead = permissions.checkPermissions(FEATURE_PERMISSIONS.READ_AUDIO_FILES);
const canManage = permissions.checkPermissions(
  FEATURE_PERMISSIONS.MANAGE_AUDIO_FILES,
);

/**
 * @openapi
 * /api/audio_files:
 *   get:
 *     tags: [Audio Library]
 *     summary: List playable audio (campus uploads + global defaults)
 *     description: Requires READ_AUDIO_FILES (the four campus staff roles).
 *     responses:
 *       200:
 *         description: Audio files.
 *         content:
 *           application/json:
 *             schema: { $ref: '#/components/schemas/ListResponse' }
 *       401: { $ref: '#/components/responses/UnauthorizedError' }
 *       403: { $ref: '#/components/responses/ForbiddenError' }
 *   post:
 *     tags: [Audio Library]
 *     summary: Add an uploaded audio file (director / office_manager / teacher)
 *     description: >
 *       Requires MANAGE_AUDIO_FILES. The binary is uploaded via the file
 *       subsystem first; `url` references it. The row is campus-scoped.
 *     responses:
 *       200: { description: Created. }
 *       403: { $ref: '#/components/responses/ForbiddenError' }
 * /api/audio_files/{id}:
 *   put:
 *     tags: [Audio Library]
 *     summary: Update an audio file (own campus; not a global default)
 *     description: Requires MANAGE_AUDIO_FILES.
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: string, format: uuid }
 *     responses:
 *       200: { description: Updated. }
 *       403: { $ref: '#/components/responses/ForbiddenError' }
 *   delete:
 *     tags: [Audio Library]
 *     summary: Delete an audio file (own campus; not a global default)
 *     description: Requires MANAGE_AUDIO_FILES.
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: string, format: uuid }
 *     responses:
 *       200: { description: Deleted. }
 *       403: { $ref: '#/components/responses/ForbiddenError' }
 */
router.get('/', canRead, wrapAsync(audio_files.list));
router.post('/', canManage, wrapAsync(audio_files.create));
router.put('/:id', canManage, wrapAsync(audio_files.update));
router.delete('/:id', canManage, wrapAsync(audio_files.remove));

export default router;
