"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.editCompanyInformation = editCompanyInformation;
const prisma_1 = __importDefault(require("../../utils/prisma"));
// ------------------------------------------------------------------------------------
// export async function joinCompany(
//   req: FastifyRequest<{ Body: JoinCompanySchema }>,
//   reply: FastifyReply
// ) {
//   const userId = req.user.id;
//   const data = req.body;
//   const company = await prisma.company.findUnique({
//     where: { id: data.companyId, active: true },
//   });
//   if (!company) {
//     return reply.code(404).send({
//       message: "Cette entreprise n'existe pas",
//     });
//   }
//   await prisma.user.update({
//     where: { id: userId },
//     data: {
//       companyId: company.id,
//     },
//   });
//   return reply.code(200).send(company);
// }
// ------------------------------------------------------------------------------------
function editCompanyInformation(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const userId = req.user.id;
        const user = yield prisma_1.default.user.findUnique({
            where: { id: userId, active: true },
        });
        if (!user) {
            return reply.code(404).send({
                message: "Cet utilisateur n'existe pas",
            });
        }
        const data = req.body;
        let company;
        const city = yield prisma_1.default.city.findUnique({
            where: {
                id: data.cityId,
            },
        });
        if (!city) {
            return reply.code(400).send({
                message: "La ville n'existe pas",
            });
        }
        if (data.contactPositionId) {
            const position = yield prisma_1.default.position.findUnique({
                where: {
                    id: data.contactPositionId,
                },
            });
            if (!position) {
                return reply.code(400).send({
                    message: "La fonction n'existe pas",
                });
            }
        }
        const turnover = yield prisma_1.default.turnover.findUnique({
            where: {
                id: data.turnoverId,
            },
        });
        if (!turnover) {
            return reply.code(400).send({
                message: "Le chiffre d'affaires n'existe pas",
            });
        }
        const nEmployees = yield prisma_1.default.nEmployees.findUnique({
            where: {
                id: data.nEmployeesId,
            },
        });
        if (!nEmployees) {
            return reply.code(400).send({
                message: "L'effectif n'existe pas",
            });
        }
        if (user.companyId) {
            company = yield prisma_1.default.company.update({
                where: { id: user.companyId },
                data: {
                    name: data.name,
                    legalForm: data.legalForm,
                    cityId: data.cityId,
                    address: data.address,
                    phoneNumber: data.phoneNumber,
                    fax: data.fax,
                    email: data.email.toLowerCase(),
                    website: data.website,
                    linkedin: data.linkedin,
                    facebook: data.facebook,
                    contactFirstName: data.contactFirstName,
                    contactLastName: data.contactLastName,
                    contactEmail: data.contactEmail,
                    contactPositionId: data.contactPositionId,
                    contactLinkedin: data.contactLinkedin,
                    contactFacebook: data.contactFacebook,
                    businessLicense: data.businessLicense,
                    numRC: data.numRC,
                    numICE: data.numICE,
                    capital: data.capital,
                    turnoverId: data.turnoverId,
                    nEmployeesId: data.nEmployeesId,
                    activitiesFamilies: data.activitiesFamilies,
                    activities: data.activities,
                    importCountries: data.importCountries,
                    exportCountries: data.exportCountries,
                },
            });
        }
        else {
            company = yield prisma_1.default.company.create({
                data: {
                    name: data.name,
                    legalForm: data.legalForm,
                    cityId: data.cityId,
                    address: data.address,
                    phoneNumber: data.phoneNumber,
                    fax: data.fax,
                    email: data.email.toLowerCase(),
                    website: data.website,
                    linkedin: data.linkedin,
                    facebook: data.facebook,
                    contactFirstName: data.contactFirstName,
                    contactLastName: data.contactLastName,
                    contactEmail: data.contactEmail,
                    contactPositionId: data.contactPositionId,
                    contactLinkedin: data.contactLinkedin,
                    contactFacebook: data.contactFacebook,
                    businessLicense: data.businessLicense,
                    numRC: data.numRC,
                    numICE: data.numICE,
                    capital: data.capital,
                    turnoverId: data.turnoverId,
                    nEmployeesId: data.nEmployeesId,
                    activitiesFamilies: data.activitiesFamilies,
                    activities: data.activities,
                    importCountries: data.importCountries,
                    exportCountries: data.exportCountries,
                    contributor: true,
                    verified: false,
                },
            });
        }
        yield prisma_1.default.user.update({
            where: { id: userId },
            data: {
                companyId: company.id,
            },
        });
        return reply.code(200).send(company);
    });
}
// ------------------------------------------------------------------------------------
// export async function joinCompanyRequests(
//   req: FastifyRequest,
//   reply: FastifyReply
// ) {
//   const userId = req.user.id;
//   const user = await prisma.user.findUnique({
//     where: { id: userId, active: true },
//   });
//   if (!user) {
//     return reply.code(404).send({
//       message: "Cet utilisateur n'existe pas",
//     });
//   }
//   if (!user.companyId) {
//     return reply.code(400).send({
//       message: "Vous n'appartenez à aucune entreprise",
//     });
//   }
//   if (!user.manager) {
//     return reply.code(400).send({
//       message: "Vous n'êtes pas le représentant de cette entreprise",
//     });
//   }
//   const users = await prisma.user.findMany({
//     where: {
//       id: { not: userId },
//       active: true,
//       companyId: user.companyId,
//     },
//     select: {
//       id: true,
//       firstName: true,
//       lastName: true,
//       position: true,
//       manager: true,
//     },
//     orderBy: {
//       firstName: "asc",
//     },
//   });
//   return reply.code(200).send(users);
// }
// // ------------------------------------------------------------------------------------
// export async function acceptDeclineJoinRequest(
//   req: FastifyRequest<{
//     Body: AcceptDeclineJoinRequestSchema;
//     Params: IdParamsSchema;
//   }>,
//   reply: FastifyReply
// ) {
//   const userId = req.user.id;
//   const id = req.params.id;
//   const data = req.body;
//   const currentUser = await prisma.user.findUnique({
//     where: { id: userId, active: true },
//   });
//   if (!currentUser) {
//     return reply.code(404).send({
//       message: "Cet utilisateur n'existe pas",
//     });
//   }
//   if (!currentUser.companyId) {
//     return reply.code(400).send({
//       message: "Vous n'appartenez à aucune entreprise",
//     });
//   }
//   if (!currentUser.manager) {
//     return reply.code(400).send({
//       message: "Vous n'êtes pas le représentant de cette entreprise",
//     });
//   }
//   const user = await prisma.user.findUnique({
//     where: { id: id, active: true },
//   });
//   if (!user) {
//     return reply.code(404).send({
//       message: "Cet utilisateur n'existe pas",
//     });
//   }
//   if (!user.companyId || user.companyId !== currentUser.companyId) {
//     return reply.code(400).send({
//       message: "La demande d'adhésion n'existe pas",
//     });
//   }
//   if (data.accepted) {
//     await prisma.user.update({
//       where: {
//         id: id,
//       },
//       data: {},
//     });
//     return reply.code(200).send({});
//   } else {
//     await prisma.user.update({
//       where: {
//         id: id,
//       },
//       data: {
//         companyId: null,
//       },
//     });
//     return reply.code(200).send({});
//   }
// }
// -----------------------------------------------------------------------------------------------------
