import express from 'express';
import { authenticate } from '../middleware/authMiddleware.js';
import { authorize } from '../middleware/authorizationMiddleware.js';
import {
  uploadBillingTemplateMiddleware,
  listTemplates,
  createTemplate,
  deleteTemplate,
  updateTemplateFields,
  createRun,
  listRuns,
  getRunDetails,
  deleteRun,
  getUserSettings,
  calculateHoursPreview,
  generateRun,
  downloadTemplatePdf,
  downloadRunPdf
} from '../controllers/billingController.js';

const router = express.Router();

router.get('/templates/:clubId', authenticate, authorize('members', 'read'), listTemplates);
router.post('/templates/:clubId', authenticate, authorize('members', 'write'), uploadBillingTemplateMiddleware, createTemplate);
router.delete('/templates/:templateId', authenticate, deleteTemplate);
router.put('/templates/:templateId/fields', authenticate, updateTemplateFields);
router.get('/templates/:templateId/pdf', authenticate, downloadTemplatePdf);

router.get('/runs/:clubId', authenticate, authorize('members', 'read'), listRuns);
router.get('/run/:runId', authenticate, getRunDetails);
router.get('/settings/:clubId', authenticate, authorize('members', 'read'), getUserSettings);
router.get('/hours-preview/:clubId', authenticate, authorize('members', 'read'), calculateHoursPreview);
router.post('/runs/:clubId', authenticate, authorize('members', 'write'), createRun);
router.post('/runs/:runId/generate', authenticate, generateRun);
router.get('/runs/:runId/download', authenticate, downloadRunPdf);
router.delete('/runs/:runId', authenticate, deleteRun);

export default router;
