import { Router } from 'express';
import { AppDataSource } from '../data-source';
import { Client } from '../entity/Client';
import { Address } from '../entity/Address';
import { TypeStudent } from '../entity/TypeStudent';
import { Student } from '../entity/Student';
import { Course } from '../entity/Course';
import { CourseCategory } from '../entity/CourseCategory';
import { TeachingMode } from '../entity/TeachingMode'; // make sure this entity exists
import { authentificationToken } from '../middleware/auth';

const router = Router();

// GET all courses
router.get('/courses', authentificationToken, async (req, res) => {
  try {
    const repo = AppDataSource.getRepository(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', authentificationToken, async (req, res) => {
  try {
    const repo = AppDataSource.getRepository(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', authentificationToken, async (req, res) => {
  try {
    const repo = AppDataSource.getRepository(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', authentificationToken, async (req, res) => {
  try {
    const repo = AppDataSource.getRepository(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', authentificationToken, async (req, res) => {
  try {
    const repo = AppDataSource.getRepository(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', authentificationToken, async (req, res) => {
  try {
    const repo = AppDataSource.getRepository(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', authentificationToken, async (req, res) => {
  try {
    const repo = AppDataSource.getRepository(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', authentificationToken, async (req, res) => {
  try {
    const repo = AppDataSource.getRepository(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', authentificationToken, async (req, res) => {
  try {
    const repo = AppDataSource.getRepository(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);
  }
});

export default router;
