import { idParamsSchema } from "../../utils/schema";
import { FastifyInstanceType } from "../../utils/types";
import { minioDocumentSchema } from "../minio/schema";
import {
  createOpportunity,
  deleteOpportunity,
  editOpportunity,
  editOpportunityActive,
  editOpportunityVerified,
  getOpportunities,
  getOpportunity,
  uploadOpportunityDocument,
} from "./controller";
import {
  opportunityActiveSchema,
  opportunitySchema,
  opportunityVerifiedSchema,
} from "./schema";

export async function adminOpportunitiesRoutes(fastify: FastifyInstanceType) {
  //-------------------------------------------------------------------------------
  fastify.get(
    "",
    {
      preHandler: (req, reply) =>
        fastify.adminAuth(req, reply, "Opportunities"),
    },
    getOpportunities
  );
  //-------------------------------------------------------------------------------
  fastify.get(
    ":id/",
    {
      preHandler: (req, reply) =>
        fastify.adminAuth(req, reply, "Opportunities"),
      schema: {
        params: idParamsSchema,
      },
    },
    getOpportunity
  );
  //-------------------------------------------------------------------------------
  fastify.post(
    "upload-document/",
    {
      preHandler: (req, reply) =>
        fastify.adminAuth(req, reply, "Opportunities"),
      schema: {
        body: minioDocumentSchema,
      },
    },
    uploadOpportunityDocument
  );
  //-------------------------------------------------------------------------------
  fastify.post(
    "",
    {
      preHandler: (req, reply) =>
        fastify.adminAuth(req, reply, "Opportunities"),
      schema: {
        body: opportunitySchema,
      },
    },
    createOpportunity
  );
  //-------------------------------------------------------------------------------
  fastify.put(
    ":id/",
    {
      preHandler: (req, reply) =>
        fastify.adminAuth(req, reply, "Opportunities"),
      schema: {
        params: idParamsSchema,
        body: opportunitySchema,
      },
    },
    editOpportunity
  );
  //-------------------------------------------------------------------------------
  fastify.put(
    ":id/active/",
    {
      preHandler: (req, reply) =>
        fastify.adminAuth(req, reply, "Opportunities"),
      schema: {
        params: idParamsSchema,
        body: opportunityVerifiedSchema,
      },
    },
    editOpportunityActive
  );
  //-------------------------------------------------------------------------------
  fastify.put(
    ":id/verified/",
    {
      preHandler: (req, reply) =>
        fastify.adminAuth(req, reply, "Opportunities"),
      schema: {
        params: idParamsSchema,
      },
    },
    editOpportunityVerified
  );
  //-------------------------------------------------------------------------------
  fastify.delete(
    ":id/",
    {
      preHandler: (req, reply) =>
        fastify.adminAuth(req, reply, "Opportunities"),
      schema: {
        params: idParamsSchema,
      },
    },
    deleteOpportunity
  );
  //-------------------------------------------------------------------------------
  fastify.log.info("opportunities routes registered");
}
