import { FastifyReply, FastifyRequest } from "fastify";
import prisma from "../../utils/prisma";
import {
  PresenationSchema,
  PrivacyPolicySchema,
  TermsOfServiceSchema,
} from "./schema";

// ------------------------------------------------------------------------------------

export async function getPresentation(
  req: FastifyRequest,
  reply: FastifyReply
) {
  const ccis = await prisma.ccis.findFirst();

  if (ccis) {
    return reply.code(200).send({ presentation: ccis.presentation });
  }

  return reply.code(200).send({ presentation: "" });
}

export async function editPresentation(
  req: FastifyRequest<{ Body: PresenationSchema }>,
  reply: FastifyReply
) {
  const data = req.body;

  const ccis = await prisma.ccis.findFirst();

  if (ccis) {
    const newCcis = await prisma.ccis.update({
      where: {
        id: ccis.id,
      },
      data: {
        presentation: data.presenation,
      },
    });
    return reply.code(200).send(newCcis);
  }

  const newCcis = await prisma.ccis.create({
    data: {
      presentation: data.presenation,
      privacyPolicy: "",
      termsOfService: "",
    },
  });
  return reply.code(200).send({ presentation: newCcis.presentation });
}

// ------------------------------------------------------------------------------------

export async function getTermsOfService(
  req: FastifyRequest,
  reply: FastifyReply
) {
  const ccis = await prisma.ccis.findFirst();

  if (ccis) {
    return reply.code(200).send({ termsOfService: ccis.termsOfService });
  }

  return reply.code(200).send({ termsOfService: "" });
}

export async function editTermsOfService(
  req: FastifyRequest<{ Body: TermsOfServiceSchema }>,
  reply: FastifyReply
) {
  const data = req.body;

  const ccis = await prisma.ccis.findFirst();

  if (ccis) {
    const newCcis = await prisma.ccis.update({
      where: {
        id: ccis.id,
      },
      data: {
        termsOfService: data.termsOfService,
      },
    });
    return reply.code(200).send(newCcis);
  }

  const newCcis = await prisma.ccis.create({
    data: {
      termsOfService: data.termsOfService,
      privacyPolicy: "",
      presentation: "",
    },
  });
  return reply.code(200).send({ termsOfService: newCcis.termsOfService });
}

// ------------------------------------------------------------------------------------

export async function getPrivacyPolicy(
  req: FastifyRequest,
  reply: FastifyReply
) {
  const ccis = await prisma.ccis.findFirst();

  if (ccis) {
    return reply.code(200).send({ privacyPolicy: ccis.privacyPolicy });
  }

  return reply.code(200).send({ privacyPolicy: "" });
}

export async function editPrivacyPolicy(
  req: FastifyRequest<{ Body: PrivacyPolicySchema }>,
  reply: FastifyReply
) {
  const data = req.body;

  const ccis = await prisma.ccis.findFirst();

  if (ccis) {
    const newCcis = await prisma.ccis.update({
      where: {
        id: ccis.id,
      },
      data: {
        privacyPolicy: data.privacyPolicy,
      },
    });
    return reply.code(200).send(newCcis);
  }

  const newCcis = await prisma.ccis.create({
    data: {
      privacyPolicy: data.privacyPolicy,
      presentation: "",
      termsOfService: "",
    },
  });
  return reply.code(200).send({ privacyPolicy: newCcis.privacyPolicy });
}
