import { Router } from 'express';
import { ProtocolController } from '../controllers/ProtocolController';
import { GetAllProtocolsUseCase } from '../../../application/usecases/GetAllProtocolsUseCase';
import { GetProtocolByIdUseCase } from '../../../application/usecases/GetProtocolByIdUseCase';
import { MongoProtocolRepository } from '../../persistence/MongoProtocolRepository';

// manual dependency injection for routes
const repository = new MongoProtocolRepository();
const getAllUseCase = new GetAllProtocolsUseCase(repository);
const getByIdUseCase = new GetProtocolByIdUseCase(repository);
const controller = new ProtocolController(getAllUseCase, getByIdUseCase);

const router = Router();

router.get('/', controller.getAll);
router.get('/:id', controller.getById);

// CRUD endpoints for Admin
router.post('/', async (req, res) => {
  try {
    await repository.save(req.body);
    res.status(201).json({ message: 'Protocolo creado correctamente' });
  } catch (error: any) {
    res.status(500).json({ error: error.message });
  }
});

router.put('/:id', async (req, res) => {
  try {
    await repository.save({ ...req.body, id: req.params.id });
    res.json({ message: 'Protocolo actualizado correctamente' });
  } catch (error: any) {
    res.status(500).json({ error: error.message });
  }
});

router.delete('/:id', async (req, res) => {
  try {
    await repository.delete(req.params.id);
    res.json({ message: 'Protocolo eliminado correctamente' });
  } catch (error: any) {
    res.status(500).json({ error: error.message });
  }
});

export default router;
