import { idParamsSchema } from "../../utils/schema";
import { FastifyInstanceType } from "../../utils/types";
import {
  createFair,
  deleteFair,
  editFair,
  getFair,
  getFairs,
} from "./controller";
import { fairSchema } from "./schema";

export async function adminFairsRoutes(fastify: FastifyInstanceType) {
  //-------------------------------------------------------------------------------
  fastify.get(
    "",
    {
      preHandler: (req, reply) => fastify.adminAuth(req, reply, "Fairs"),
    },
    getFairs
  );
  //-------------------------------------------------------------------------------
  fastify.get(
    ":id/",
    {
      preHandler: (req, reply) => fastify.adminAuth(req, reply, "Fairs"),
      schema: {
        params: idParamsSchema,
      },
    },
    getFair
  );
  //-------------------------------------------------------------------------------
  fastify.post(
    "",
    {
      preHandler: (req, reply) => fastify.adminAuth(req, reply, "Fairs"),
      schema: {
        body: fairSchema,
      },
    },
    createFair
  );
  //-------------------------------------------------------------------------------
  fastify.put(
    ":id/",
    {
      preHandler: (req, reply) => fastify.adminAuth(req, reply, "Fairs"),
      schema: {
        params: idParamsSchema,
        body: fairSchema,
      },
    },
    editFair
  );
  //-------------------------------------------------------------------------------
  fastify.delete(
    ":id/",
    {
      preHandler: (req, reply) => fastify.adminAuth(req, reply, "Fairs"),
      schema: {
        params: idParamsSchema,
      },
    },
    deleteFair
  );
  //-------------------------------------------------------------------------------
  fastify.log.info("fairs routes registered");
}
