import { FastifyReply, FastifyRequest } from "fastify";
import prisma from "../../utils/prisma";
import {
  ActivityFamiliySchema,
  ActivitySchema,
  CategorySchema,
  CitySchema,
  CollegeSchema,
  CommissionSchema,
  ExpertiseAreaSchema,
  InterestAreaSchema,
  MandateSchema,
  NEmployeesSchema,
  PositionSchema,
  ProvinceSchema,
  TurnoverSchema,
} from "./schema";
import { IdParamsSchema } from "../../utils/schema";

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

export async function cities(req: FastifyRequest, reply: FastifyReply) {
  const cities = await prisma.city.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(cities);
}

export async function addCity(
  req: FastifyRequest<{ Body: CitySchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const city = await prisma.city.create({
    data: {
      name: data.name,
    },
  });
  return reply.code(200).send(city);
}

export async function editCity(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: CitySchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const city = await prisma.city.findUnique({
    where: {
      id: id,
    },
  });

  if (!city) {
    return reply.code(404).send({
      message: "La ville n'existe pas",
    });
  }

  const newCity = await prisma.city.update({
    where: { id: id },
    data: {
      name: data.name,
    },
  });

  return reply.code(200).send(newCity);
}

export async function deleteCity(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const city = await prisma.city.findUnique({
    where: {
      id: id,
    },
  });

  if (!city) {
    return reply.code(404).send({
      message: "La ville n'existe pas",
    });
  }

  try {
    await prisma.city.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer cette ville",
    });
  }

  return reply.code(200).send({});
}

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

export async function activitiesFamilies(
  req: FastifyRequest,
  reply: FastifyReply
) {
  const activitiesFamilies = await prisma.activityFamily.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(activitiesFamilies);
}

export async function addActivityFamily(
  req: FastifyRequest<{ Body: ActivityFamiliySchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const activityFamily = await prisma.activityFamily.create({
    data: {
      name: data.name,
    },
  });
  return reply.code(200).send(activityFamily);
}

export async function editActivityFamily(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: ActivityFamiliySchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const activityFamily = await prisma.activityFamily.findUnique({
    where: {
      id: id,
    },
  });

  if (!activityFamily) {
    return reply.code(404).send({
      message: "La famille d'activité n'existe pas",
    });
  }

  const newActivityFamily = await prisma.activityFamily.update({
    where: { id: id },
    data: {
      name: data.name,
    },
  });

  return reply.code(200).send(newActivityFamily);
}

export async function deleteActivityFamily(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const activityFamily = await prisma.activityFamily.findUnique({
    where: {
      id: id,
    },
  });

  if (!activityFamily) {
    return reply.code(404).send({
      message: "La famille d'activité n'existe pas",
    });
  }

  try {
    await prisma.activityFamily.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer cette famille d'activité",
    });
  }

  return reply.code(200).send({});
}

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

export async function activities(req: FastifyRequest, reply: FastifyReply) {
  const activities = await prisma.activity.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(activities);
}

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

  const activityFamily = await prisma.activityFamily.findUnique({
    where: {
      id: data.activityFamilyId,
    },
  });

  if (!activityFamily) {
    return reply.code(404).send({
      message: "La famille d'activité n'existe pas",
    });
  }

  const activity = await prisma.activity.create({
    data: {
      activityFamilyId: activityFamily.id,
      name: data.name,
    },
  });
  return reply.code(200).send(activity);
}

export async function editActivity(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: ActivitySchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const activity = await prisma.activity.findUnique({
    where: {
      id: id,
    },
  });

  if (!activity) {
    return reply.code(404).send({
      message: "L'activité n'existe pas",
    });
  }

  const activityFamily = await prisma.activityFamily.findUnique({
    where: {
      id: data.activityFamilyId,
    },
  });

  if (!activityFamily) {
    return reply.code(404).send({
      message: "La famille d'activité n'existe pas",
    });
  }

  const newActivity = await prisma.activity.update({
    where: { id: id },
    data: {
      activityFamilyId: activityFamily.id,
      name: data.name,
    },
  });

  return reply.code(200).send(newActivity);
}

export async function deleteActivity(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const activity = await prisma.activity.findUnique({
    where: {
      id: id,
    },
  });

  if (!activity) {
    return reply.code(404).send({
      message: "L'activité n'existe pas",
    });
  }

  try {
    await prisma.activity.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer cette activité",
    });
  }

  return reply.code(200).send({});
}

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

export async function turnovers(req: FastifyRequest, reply: FastifyReply) {
  const turnovers = await prisma.turnover.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(turnovers);
}

export async function addTurnover(
  req: FastifyRequest<{ Body: TurnoverSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const turnover = await prisma.turnover.create({
    data: {
      name: data.name,
    },
  });
  return reply.code(200).send(turnover);
}

export async function editTurnover(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: TurnoverSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const turnover = await prisma.turnover.findUnique({
    where: {
      id: id,
    },
  });

  if (!turnover) {
    return reply.code(404).send({
      message: "Le chiffre d'affaires n'existe pas",
    });
  }

  const newTurnover = await prisma.turnover.update({
    where: { id: id },
    data: {
      name: data.name,
    },
  });

  return reply.code(200).send(newTurnover);
}

export async function deleteTurnover(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const turnover = await prisma.turnover.findUnique({
    where: {
      id: id,
    },
  });

  if (!turnover) {
    return reply.code(404).send({
      message: "Le chiffre d'affaires n'existe pas",
    });
  }

  try {
    await prisma.turnover.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer ce chiffre d'affaires",
    });
  }

  return reply.code(200).send({});
}

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

export async function positions(req: FastifyRequest, reply: FastifyReply) {
  const positions = await prisma.position.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(positions);
}

export async function addPosition(
  req: FastifyRequest<{ Body: PositionSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const position = await prisma.position.create({
    data: {
      name: data.name,
    },
  });
  return reply.code(200).send(position);
}

export async function editPosition(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: PositionSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const position = await prisma.position.findUnique({
    where: {
      id: id,
    },
  });

  if (!position) {
    return reply.code(404).send({
      message: "La fonction n'existe pas",
    });
  }

  const newPosition = await prisma.position.update({
    where: { id: id },
    data: {
      name: data.name,
    },
  });

  return reply.code(200).send(newPosition);
}

export async function deletePosition(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const position = await prisma.position.findUnique({
    where: {
      id: id,
    },
  });

  if (!position) {
    return reply.code(404).send({
      message: "La fonction n'existe pas",
    });
  }

  try {
    await prisma.position.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer cette fonction",
    });
  }

  return reply.code(200).send({});
}

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

export async function nEmployees(req: FastifyRequest, reply: FastifyReply) {
  const nEmployees = await prisma.nEmployees.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(nEmployees);
}

export async function addNEmployees(
  req: FastifyRequest<{ Body: NEmployeesSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const nEmployees = await prisma.nEmployees.create({
    data: {
      name: data.name,
    },
  });
  return reply.code(200).send(nEmployees);
}

export async function editNEmployees(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: NEmployeesSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const nEmployees = await prisma.nEmployees.findUnique({
    where: {
      id: id,
    },
  });

  if (!nEmployees) {
    return reply.code(404).send({
      message: "L'effectif n'existe pas",
    });
  }

  const newNEmployees = await prisma.nEmployees.update({
    where: { id: id },
    data: {
      name: data.name,
    },
  });

  return reply.code(200).send(newNEmployees);
}

export async function deleteNEmployees(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const nEmployees = await prisma.nEmployees.findUnique({
    where: {
      id: id,
    },
  });

  if (!nEmployees) {
    return reply.code(404).send({
      message: "L'effectif n'existe pas",
    });
  }

  try {
    await prisma.nEmployees.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer cet effectif",
    });
  }

  return reply.code(200).send({});
}

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

export async function expertiseAreas(req: FastifyRequest, reply: FastifyReply) {
  const expertiseAreas = await prisma.expertiseArea.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(expertiseAreas);
}

export async function addExpertiseArea(
  req: FastifyRequest<{ Body: ExpertiseAreaSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const expertiseArea = await prisma.expertiseArea.create({
    data: {
      name: data.name,
    },
  });
  return reply.code(200).send(expertiseArea);
}

export async function editExpertiseArea(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: ExpertiseAreaSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const expertiseArea = await prisma.expertiseArea.findUnique({
    where: {
      id: id,
    },
  });

  if (!expertiseArea) {
    return reply.code(404).send({
      message: "Le domaine d'expertise n'existe pas",
    });
  }

  const newExpertiseAreay = await prisma.expertiseArea.update({
    where: { id: id },
    data: {
      name: data.name,
    },
  });

  return reply.code(200).send(newExpertiseAreay);
}

export async function deleteExpertiseArea(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const expertiseArea = await prisma.expertiseArea.findUnique({
    where: {
      id: id,
    },
  });

  if (!expertiseArea) {
    return reply.code(404).send({
      message: "Le domaine d'expertise n'existe pas",
    });
  }

  try {
    await prisma.expertiseArea.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer ce domaine d'expertise",
    });
  }

  return reply.code(200).send({});
}

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

export async function interestAreas(req: FastifyRequest, reply: FastifyReply) {
  const interestArea = await prisma.interestArea.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(interestArea);
}

export async function addInterestArea(
  req: FastifyRequest<{ Body: InterestAreaSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const interestArea = await prisma.interestArea.create({
    data: {
      name: data.name,
    },
  });
  return reply.code(200).send(interestArea);
}

export async function editInterestArea(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: InterestAreaSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const interestArea = await prisma.interestArea.findUnique({
    where: {
      id: id,
    },
  });

  if (!interestArea) {
    return reply.code(404).send({
      message: "Le centre d'intérêt n'existe pas",
    });
  }

  const newInterestArea = await prisma.interestArea.update({
    where: { id: id },
    data: {
      name: data.name,
    },
  });

  return reply.code(200).send(newInterestArea);
}

export async function deleteInterestArea(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const interestArea = await prisma.interestArea.findUnique({
    where: {
      id: id,
    },
  });

  if (!interestArea) {
    return reply.code(404).send({
      message: "Le centre d'intérêt n'existe pas",
    });
  }

  try {
    await prisma.interestArea.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer ce centre d'intérêt",
    });
  }

  return reply.code(200).send({});
}

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

export async function provinces(req: FastifyRequest, reply: FastifyReply) {
  const provinces = await prisma.province.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(provinces);
}

export async function addProvince(
  req: FastifyRequest<{ Body: ProvinceSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const province = await prisma.province.create({
    data: {
      name: data.name,
    },
  });
  return reply.code(200).send(province);
}

export async function editProvince(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: ProvinceSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const province = await prisma.province.findUnique({
    where: {
      id: id,
    },
  });

  if (!province) {
    return reply.code(404).send({
      message: "La province n'existe pas",
    });
  }

  const newProvince = await prisma.province.update({
    where: { id: id },
    data: {
      name: data.name,
    },
  });

  return reply.code(200).send(newProvince);
}

export async function deleteProvince(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const province = await prisma.province.findUnique({
    where: {
      id: id,
    },
  });

  if (!province) {
    return reply.code(404).send({
      message: "La province n'existe pas",
    });
  }

  try {
    await prisma.province.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer cette province",
    });
  }

  return reply.code(200).send({});
}

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

export async function categories(req: FastifyRequest, reply: FastifyReply) {
  const categories = await prisma.category.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(categories);
}

export async function addCategory(
  req: FastifyRequest<{ Body: CategorySchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const category = await prisma.category.create({
    data: {
      name: data.name,
      nameAr: data.nameAr,
    },
  });
  return reply.code(200).send(category);
}

export async function editCategory(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: CategorySchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const category = await prisma.category.findUnique({
    where: {
      id: id,
    },
  });

  if (!category) {
    return reply.code(404).send({
      message: "La catégorie n'existe pas",
    });
  }

  const newCategory = await prisma.category.update({
    where: { id: id },
    data: {
      name: data.name,
    },
  });

  return reply.code(200).send(newCategory);
}

export async function deleteCategory(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const category = await prisma.category.findUnique({
    where: {
      id: id,
    },
  });

  if (!category) {
    return reply.code(404).send({
      message: "La catégorie n'existe pas",
    });
  }

  try {
    await prisma.category.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer cette catégorie",
    });
  }

  return reply.code(200).send({});
}

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

export async function commissions(req: FastifyRequest, reply: FastifyReply) {
  const commissions = await prisma.commission.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(commissions);
}

export async function addCommission(
  req: FastifyRequest<{ Body: CommissionSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const commission = await prisma.commission.create({
    data: {
      name: data.name,
      description: data.description,
    },
  });
  return reply.code(200).send(commission);
}

export async function editCommission(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: CommissionSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const commission = await prisma.commission.findUnique({
    where: {
      id: id,
    },
  });

  if (!commission) {
    return reply.code(404).send({
      message: "La commission n'existe pas",
    });
  }

  const newCommission = await prisma.commission.update({
    where: { id: id },
    data: {
      name: data.name,
      description: data.description,
    },
  });

  return reply.code(200).send(newCommission);
}

export async function deleteCommission(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const commission = await prisma.commission.findUnique({
    where: {
      id: id,
    },
  });

  if (!commission) {
    return reply.code(404).send({
      message: "La commission n'existe pas",
    });
  }

  try {
    await prisma.commission.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer cette commission",
    });
  }

  return reply.code(200).send({});
}

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

export async function mandates(req: FastifyRequest, reply: FastifyReply) {
  const mandates = await prisma.mandate.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(mandates);
}

export async function addMandate(
  req: FastifyRequest<{ Body: MandateSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const mandate = await prisma.mandate.create({
    data: {
      name: data.name,
    },
  });
  return reply.code(200).send(mandate);
}

export async function editMandate(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: MandateSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const mandate = await prisma.mandate.findUnique({
    where: {
      id: id,
    },
  });

  if (!mandate) {
    return reply.code(404).send({
      message: "Le mandat n'existe pas",
    });
  }

  const newMandate = await prisma.mandate.update({
    where: { id: id },
    data: {
      name: data.name,
    },
  });

  return reply.code(200).send(newMandate);
}

export async function deleteMandate(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const mandate = await prisma.mandate.findUnique({
    where: {
      id: id,
    },
  });

  if (!mandate) {
    return reply.code(404).send({
      message: "Le mandat n'existe pas",
    });
  }

  try {
    await prisma.mandate.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer ce mandat",
    });
  }

  return reply.code(200).send({});
}

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

export async function colleges(req: FastifyRequest, reply: FastifyReply) {
  const colleges = await prisma.college.findMany({
    orderBy: { name: "asc" },
  });
  return reply.code(200).send(colleges);
}

export async function addCollege(
  req: FastifyRequest<{ Body: CollegeSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const college = await prisma.college.create({
    data: {
      name: data.name,
    },
  });
  return reply.code(200).send(college);
}

export async function editCollege(
  req: FastifyRequest<{ Params: IdParamsSchema; Body: CollegeSchema }>,
  reply: FastifyReply
) {
  const data = req.body;
  const id = req.params.id;

  const college = await prisma.college.findUnique({
    where: {
      id: id,
    },
  });

  if (!college) {
    return reply.code(404).send({
      message: "Le collège n'existe pas",
    });
  }

  const newCollege = await prisma.college.update({
    where: { id: id },
    data: {
      name: data.name,
    },
  });

  return reply.code(200).send(newCollege);
}

export async function deleteCollege(
  req: FastifyRequest<{ Params: IdParamsSchema }>,
  reply: FastifyReply
) {
  const id = req.params.id;

  const college = await prisma.college.findUnique({
    where: {
      id: id,
    },
  });

  if (!college) {
    return reply.code(404).send({
      message: "Le collège n'existe pas",
    });
  }

  try {
    await prisma.college.delete({
      where: { id: id },
    });
  } catch {
    return reply.code(400).send({
      message: "Vous ne pouvez pas supprimer ce collège",
    });
  }

  return reply.code(200).send({});
}
