import { idParamsSchema } from "../../utils/schema";
import { FastifyInstanceType } from "../../utils/types";
import {
  createOpportunity,
  deleteOpportunity,
  getMyOpportunities,
  getOpportunities,
  getOpportunity,
  opportunityComment,
  opportunityLike,
  editOpportunityActive,
  editOpportunity,
} from "./controller";
import {
  opportunityCommentSchema,
  opportunitySchema,
  opportunityActiveSchema,
} from "./schema";

export async function opportunitiesRoutes(fastify: FastifyInstanceType) {
  //-------------------------------------------------------------------------------
  fastify.get(
    "",
    {
      preHandler: (req, reply) => fastify.auth(req, reply, false),
    },
    getOpportunities
  );
  //-------------------------------------------------------------------------------
  fastify.get(
    "mine/",
    {
      preHandler: (req, reply) => fastify.auth(req, reply, false),
    },
    getMyOpportunities
  );
  //-------------------------------------------------------------------------------
  fastify.get(
    ":id/",
    {
      preHandler: (req, reply) => fastify.auth(req, reply, false),
      schema: {
        params: idParamsSchema,
      },
    },
    getOpportunity
  );
  //-------------------------------------------------------------------------------
  fastify.post(
    "",
    {
      preHandler: (req, reply) => fastify.auth(req, reply, false),
      schema: {
        body: opportunitySchema,
      },
    },
    createOpportunity
  );
  //-------------------------------------------------------------------------------
  fastify.post(
    ":id/",
    {
      preHandler: (req, reply) => fastify.auth(req, reply, false),
      schema: {
        params: idParamsSchema,
        body: opportunitySchema,
      },
    },
    editOpportunity
  );
  //-------------------------------------------------------------------------------
  fastify.put(
    ":id/active/",
    {
      preHandler: (req, reply) => fastify.auth(req, reply, false),
      schema: {
        params: idParamsSchema,
        body: opportunityActiveSchema,
      },
    },
    editOpportunityActive
  );
  //-------------------------------------------------------------------------------
  fastify.delete(
    ":id/",
    {
      preHandler: (req, reply) => fastify.auth(req, reply, false),
      schema: {
        params: idParamsSchema,
      },
    },
    deleteOpportunity
  );
  //-------------------------------------------------------------------------------
  fastify.post(
    ":id/like/",
    {
      preHandler: (req, reply) => fastify.auth(req, reply, false),
      schema: {
        params: idParamsSchema,
      },
    },
    opportunityLike
  );
  //-------------------------------------------------------------------------------
  fastify.post(
    ":id/comment/",
    {
      preHandler: (req, reply) => fastify.auth(req, reply, false),
      schema: {
        params: idParamsSchema,
        body: opportunityCommentSchema,
      },
    },
    opportunityComment
  );

  //-------------------------------------------------------------------------------
  fastify.log.info("opportunities routes registered");
}
