import express from 'express';
import { BlogController } from '../controllers/BlogController.js';
import { authenticateToken, checkOwnership } from '../middlewares/authMiddleware.js';
import { BlogRepository } from '../../infrastructure/repositories/BlogRepository.js';

const router = express.Router();
const blogController = new BlogController();
const blogRepository = new BlogRepository();

// Publikus route-ok
router.get('/', (req, res) => blogController.getAll(req, res));
router.get('/:id', (req, res) => blogController.getById(req, res));

// Védett route-ok
router.post('/', 
  authenticateToken, 
  (req, res) => blogController.create(req, res)
);

router.put('/:id', 
  authenticateToken, 
  checkOwnership(async (req) => {
    const blog = await blogRepository.findById(parseInt(req.params.id));
    return blog?.authorId;
  }),
  (req, res) => blogController.update(req, res)
);

router.delete('/:id', 
  authenticateToken, 
  checkOwnership(async (req) => {
    const blog = await blogRepository.findById(parseInt(req.params.id));
    return blog?.authorId;
  }),
  (req, res) => blogController.delete(req, res)
);

export default router;
