import { Router } from 'express';
import { AppDataSource } from '../data-source';
import { Student } from '../entity/Student';
import { authentificationToken } from '../middleware/auth';
import { TypeStudent } from '../entity/TypeStudent';
import { TitleStudent } from '../entity/TitleStudent';

const router = Router();

router.post('/addStudent', authentificationToken, async (req, res) => {
  try {
    const repo = AppDataSource.getRepository(Student);
    const student = repo.create(req.body);
    const results = await repo.save(student);
    res.send(results);
  } catch (error) {
    console.error(error);
    res.sendStatus(500);
  }
});

router.put('/updateStudent/:id', authentificationToken, async (req, res) => {
  try {
    const repo = AppDataSource.getRepository(Student);
    const student = await repo.findOne({
      where: { id: parseInt(req.params.id) },
      relations: ['TypeStudent', 'TitleStudent']
    });
    repo.merge(student, req.body);
    const results = await repo.save(student);
    res.send(results);
  } catch (error) {
    console.error(error);
    res.sendStatus(500);
  }
});

router.delete('/deletestudent/:id', authentificationToken, async (req, res) => {
  try {
    const repo = AppDataSource.getRepository(Student);
    const s = await repo.findOne({ where: { id: parseInt(req.params.id) } });
    s.enable = !s.enable;
    await repo.save(s);
    res.send(true);
  } catch (error) {
    console.error(error);
    res.send(error);
  }
});
router.get('/studenttypes', authentificationToken, async function (req, res) {
   
        let TypeStudentTypeRepository = AppDataSource.getRepository(TypeStudent);
        let TypeStudents = await TypeStudentTypeRepository.find();
        res.send(TypeStudents);
});

router.put('/updatestudenttype/:id', authentificationToken, async function (req, res) {
    
         const TypeStudentTypeRepository = AppDataSource.getRepository(TypeStudent);
         const typeStudent = await TypeStudentTypeRepository.findOneBy({ id: parseInt(req.params.id) });
         TypeStudentTypeRepository.merge(typeStudent, req.body);
         const results = await TypeStudentTypeRepository.save(typeStudent);
         res.send(results);
 });

 router.post('/addstudenttype', authentificationToken, async function (req, res) {
    
         const TypeStudentTypeRepository = AppDataSource.getRepository(TypeStudent);
         const typeStudent = await TypeStudentTypeRepository.create(req.body);
         const results = await TypeStudentTypeRepository.save(typeStudent);
         res.send(results);
 });

router.get('/studenttitles', authentificationToken, async function (req, res) {
   
        let TitleStudentRepository = AppDataSource.getRepository(TitleStudent);
        let TitleStudents = await TitleStudentRepository.find();
        res.send(TitleStudents);
});

router.put('/updatestudenttitle/:id', authentificationToken, async function (req, res) {
         const TitleStudentRepository = AppDataSource.getRepository(TitleStudent);
         const titleStudent = await TitleStudentRepository.findOneBy({ id: parseInt(req.params.id) });
         TitleStudentRepository.merge(titleStudent, req.body);
         const results = await TitleStudentRepository.save(titleStudent);
         res.send(results);
 });

router.post('/addstudenttitle', authentificationToken, async function (req, res) { 
         const TitleStudentRepository = AppDataSource.getRepository(TitleStudent);
         const titleStudent = await TitleStudentRepository.create(req.body);
         const results = await TitleStudentRepository.save(titleStudent);
         res.send(results);
});


export default router;