const express = require('express');
const VideoController = require('../controllers/VideoController');
const UploadMiddleware = require('../middleware/uploadMiddleware');

const {
  authenticate,
  authenticate_noerror,
  authenticate_noerror_WithUser,
  authenticate_noerror_WithUserLean,
} = require('../middleware/authenticate');


const router = express.Router();

// Configurazione
const UPLOAD_PATH = process.env.VIDEO_UPLOAD_PATH || 'uploads/videos';

// Istanze
const videoController = new VideoController(UPLOAD_PATH);
const uploadMiddleware = new UploadMiddleware(UPLOAD_PATH);

// ============ FOLDER ROUTES ============
router.get('/folders', authenticate, videoController.getFolders);
router.post('/folders', authenticate, videoController.createFolder);
router.put('/folders/:folderPath(*)', authenticate, videoController.renameFolder);
router.delete('/folders/:folderPath(*)', authenticate, videoController.deleteFolder);

// ============ VIDEO ROUTES ============
router.get('/videos', authenticate, videoController.getVideos);
router.get('/videos/:folder/:filename', authenticate, videoController.getVideo);

// Upload
router.post(
  '/videos/upload',
  uploadMiddleware.single('video'),
  videoController.uploadVideo
);

router.post(
  '/videos/upload-multiple',
  uploadMiddleware.multiple('videos', 10),
  videoController.uploadVideos
);

// Modifica
router.put('/videos/:folder/:filename/rename', authenticate, videoController.renameVideo);
router.put('/videos/:folder/:filename/move', authenticate, videoController.moveVideo);

// Elimina
router.delete('/videos/:folder/:filename', authenticate, videoController.deleteVideo);

// Stream
router.get('/stream/:folder/:filename', authenticate, videoController.streamVideo);

// Error Handler
router.use(VideoController.errorHandler);

module.exports = router;