"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const data_source_1 = require("../data-source");
const Course_1 = require("../entity/Course");
const CourseCategory_1 = require("../entity/CourseCategory");
const TeachingMode_1 = require("../entity/TeachingMode"); // make sure this entity exists
const auth_1 = require("../middleware/auth");
const router = (0, express_1.Router)();
// GET all courses
router.get('/courses', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(Course_1.Course);
        const courses = await repo.find({ relations: ['TypeStudent', 'CourseCategory'] });
        res.send(courses);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
// POST a new course
router.post('/addcourse', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(Course_1.Course);
        const course = repo.create(req.body);
        const result = await repo.save(course);
        res.send(result);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
// PUT update existing course
router.put('/updatecourse/:id', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(Course_1.Course);
        const course = await repo.findOne({
            where: { id: parseInt(req.params.id) },
            relations: ['TypeStudent', 'CourseCategory']
        });
        if (!course)
            res.status(404).json({ message: 'Course not found' });
        repo.merge(course, req.body);
        const result = await repo.save(course);
        res.send(result);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
// GET all course categories
router.get('/coursecategories', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(CourseCategory_1.CourseCategory);
        const categories = await repo.find();
        res.send(categories);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
// POST a new course category
router.post('/addcoursecategorie', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(CourseCategory_1.CourseCategory);
        const category = repo.create(req.body);
        const result = await repo.save(category);
        res.send(result);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
// PUT update existing course category
router.put('/updatecoursecategorie/:id', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(CourseCategory_1.CourseCategory);
        const category = await repo.findOneBy({ id: parseInt(req.params.id) });
        if (!category) {
            res.status(404).json({ message: 'Course category not found' });
            return;
        }
        repo.merge(category, req.body);
        const result = await repo.save(category);
        res.send(result);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
// GET all teaching modes
router.get('/teachingmodes', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(TeachingMode_1.TeachingMode);
        const modes = await repo.find();
        res.send(modes);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
// POST a new teaching mode
router.post('/addteachingmode', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(TeachingMode_1.TeachingMode);
        const mode = repo.create(req.body);
        const result = await repo.save(mode);
        res.send(result);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
// PUT update existing teaching mode
router.put('/updateteachingmode/:id', auth_1.authentificationToken, async (req, res) => {
    try {
        const repo = data_source_1.AppDataSource.getRepository(TeachingMode_1.TeachingMode);
        const mode = await repo.findOneBy({ id: parseInt(req.params.id) });
        if (!mode) {
            res.status(404).json({ message: 'Teaching mode not found' });
            return;
        }
        repo.merge(mode, req.body);
        const result = await repo.save(mode);
        res.send(result);
    }
    catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
});
exports.default = router;
