"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const data_source_1 = require("../data-source");
const Client_1 = require("../entity/Client");
const Address_1 = require("../entity/Address");
const TypeStudent_1 = require("../entity/TypeStudent");
const Student_1 = require("../entity/Student");
const util_1 = require("util");
const auth_1 = require("../middleware/auth");
const ClientType_1 = require("../entity/ClientType");
const router = (0, express_1.Router)();
router.get('/clients', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(Client_1.Client);
        const clients = await repo.find({
            relations: [
                'Students',
                'ClientType',
                'Addresses',
                'Students.TypeStudent',
                'Students.TitleStudent',
                'Classes',
                'Classes.ClassesDetails.Students',
                'Classes.ClassesDetails',
                'Classes.ClassesDetails.Course'
            ]
        });
        res.send(clients);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
router.get('/client/:id', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(Client_1.Client);
        const client = await repo.findOne({
            where: { id: parseInt(req.params.id) },
            relations: [
                'Students',
                'ClientType',
                'Addresses',
                'Students.TypeStudent',
                'Students.TitleStudent',
                'Students.Client',
                'Classes',
                'Classes.User',
                'Classes.TeachingMode',
                'Classes.ClassesDetails',
                'Classes.ClassesDetails.Students',
                'Classes.ClassesDetails.Course'
            ]
        });
        res.send(client);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
router.post('/addclient', auth_1.authentificationToken, async (req, res) => {
    try {
        const clientRepo = data_source_1.AppDataSource.getRepository(Client_1.Client);
        const studentRepo = data_source_1.AppDataSource.getRepository(Student_1.Student);
        const addressRepo = data_source_1.AppDataSource.getRepository(Address_1.Address);
        const typeStudentRepo = data_source_1.AppDataSource.getRepository(TypeStudent_1.TypeStudent);
        const client = clientRepo.create(req.body)[0];
        const results = await clientRepo.save(client);
        const typeStudent = await typeStudentRepo.findOne({ where: { name: 'Provider' } });
        client.Students = results.Students;
        client.Students[0].Client = { id: results.id };
        client.Students[0].TypeStudent = { id: typeStudent?.id };
        client.Addresses = results.Addresses;
        client.Addresses[0].Client = { id: results.id };
        await studentRepo.save(client.Students);
        await addressRepo.save(client.Addresses);
        res.send(results);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
router.put('/updateclient/:id', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(Client_1.Client);
        const addressRepo = data_source_1.AppDataSource.getRepository(Address_1.Address);
        const client = await repo.findOne({
            where: { id: parseInt(req.params.id) },
            relations: ['Students', 'Students.TypeStudent', 'Students.TitleStudent']
        });
        repo.merge(client, req.body);
        const results = await repo.save(client);
        for (const a of req.body.Addresses) {
            if ((0, util_1.isUndefined)(a.id) || a.id === null) {
                const address = addressRepo.create(a)[0];
                address.Client = results;
                await addressRepo.save(address);
            }
            else {
                const address = await addressRepo.findOne({ where: { id: a.id } });
                addressRepo.merge(address, a);
                await addressRepo.save(address);
            }
        }
        res.send(results);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
router.delete('/deleteclient/:id', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(Client_1.Client);
        const client = await repo.findOne({
            where: { id: parseInt(req.params.id) },
            relations: ['Students', 'Students.TypeStudent', 'Students.TitleStudent']
        });
        client.enable = false;
        const results = await repo.save(client);
        res.send(results);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
router.post('/addaddress', auth_1.authentificationToken, async (req, res) => {
    try {
        const addressRepo = data_source_1.AppDataSource.getRepository(Address_1.Address);
        const address = addressRepo.create(req.body);
        const results = await addressRepo.save(address);
        res.send(results);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
router.put('/updateaddresses', auth_1.authentificationToken, function (req, res) {
    try {
        const addressRepo = data_source_1.AppDataSource.getRepository(Address_1.Address);
        let Addresses = req.body;
        Addresses.forEach(async (a) => {
            const address = await addressRepo.findOne({
                where: { id: a.id },
            });
            addressRepo.merge(address, a);
            await addressRepo.save(address);
        });
        res.send(true);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
router.get('/clienttypes', async function (req, res) {
    try {
        const clientTypeRepository = data_source_1.AppDataSource.getRepository(ClientType_1.ClientType);
        let clientTypes = await clientTypeRepository.find();
        res.send(clientTypes);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
router.post('/addclienttype', auth_1.authentificationToken, async function (req, res) {
    const ClientTypeTypeRepository = data_source_1.AppDataSource.getRepository(ClientType_1.ClientType);
    const clientType = await ClientTypeTypeRepository.create(req.body);
    const results = await ClientTypeTypeRepository.save(clientType);
    res.send(results);
});
router.put('/updateclienttype/:id', auth_1.authentificationToken, async function (req, res) {
    const ClientTypeTypeRepository = data_source_1.AppDataSource.getRepository(ClientType_1.ClientType);
    const clientType = await ClientTypeTypeRepository.findOneBy({ id: parseInt(req.params.id) });
    ClientTypeTypeRepository.merge(clientType, req.body);
    const results = await ClientTypeTypeRepository.save(clientType);
    res.send(results);
});
exports.default = router;
