import { idParamsSchema } from "../../utils/schema";
import { FastifyInstanceType } from "../../utils/types";
import {
  createSubAdmin,
  deleteSubAdmin,
  editSubAdmin,
  editSubAdminActive,
  getSubAdmins,
  editSubAdminPassword,
} from "./controller";
import {
  editSubAdminPasswordSchema,
  editSubAdminSchema,
  subAdminActiveSchema,
  subAdminSchema,
} from "./schema";

export async function adminSubAdminsRoutes(fastify: FastifyInstanceType) {
  //-------------------------------------------------------------------------------
  fastify.get(
    "",
    {
      preHandler: (req, reply) => fastify.adminAuth(req, reply, null),
    },
    getSubAdmins
  );
  //-------------------------------------------------------------------------------
  fastify.post(
    "",
    {
      preHandler: (req, reply) => fastify.adminAuth(req, reply, null),
      schema: {
        body: subAdminSchema,
      },
    },
    createSubAdmin
  );
  //-------------------------------------------------------------------------------
  fastify.put(
    ":id/",
    {
      preHandler: (req, reply) => fastify.adminAuth(req, reply, null),
      schema: {
        params: idParamsSchema,
        body: editSubAdminSchema,
      },
    },
    editSubAdmin
  );
  //-------------------------------------------------------------------------------
  fastify.put(
    ":id/active/",
    {
      preHandler: (req, reply) => fastify.adminAuth(req, reply, null),
      schema: {
        params: idParamsSchema,
        body: subAdminActiveSchema,
      },
    },
    editSubAdminActive
  );
  //-------------------------------------------------------------------------------
  fastify.put(
    ":id/password/",
    {
      preHandler: (req, reply) => fastify.adminAuth(req, reply, null),
      schema: {
        params: idParamsSchema,
        body: editSubAdminPasswordSchema,
      },
    },
    editSubAdminPassword
  );
  //-------------------------------------------------------------------------------
  fastify.delete(
    ":id/",
    {
      preHandler: (req, reply) => fastify.adminAuth(req, reply, null),
      schema: {
        params: idParamsSchema,
      },
    },
    deleteSubAdmin
  );
  //-------------------------------------------------------------------------------
  fastify.log.info("subAdmins routes registered");
}
