import { Router } from 'express';
import { reportsController } from './reports.controller.js';
import { authenticate, requireRoles } from '../../shared/middleware/auth.middleware.js';

const router = Router();

// All routes require authentication
router.use(authenticate);

// ============================================================================
// QUICK REPORTS (direct access without execution record)
// ============================================================================

router.get('/quick/trial-balance',
  requireRoles('admin', 'manager', 'accountant', 'super_admin'),
  (req, res, next) => reportsController.getTrialBalance(req, res, next)
);

router.get('/quick/general-ledger',
  requireRoles('admin', 'manager', 'accountant', 'super_admin'),
  (req, res, next) => reportsController.getGeneralLedger(req, res, next)
);

// ============================================================================
// DEFINITIONS
// ============================================================================

// List all report definitions
router.get('/definitions',
  requireRoles('admin', 'manager', 'accountant', 'super_admin'),
  (req, res, next) => reportsController.findAllDefinitions(req, res, next)
);

// Get specific definition
router.get('/definitions/:id',
  requireRoles('admin', 'manager', 'accountant', 'super_admin'),
  (req, res, next) => reportsController.findDefinitionById(req, res, next)
);

// Create custom definition (admin only)
router.post('/definitions',
  requireRoles('admin', 'super_admin'),
  (req, res, next) => reportsController.createDefinition(req, res, next)
);

// ============================================================================
// EXECUTIONS
// ============================================================================

// Execute a report
router.post('/execute',
  requireRoles('admin', 'manager', 'accountant', 'super_admin'),
  (req, res, next) => reportsController.executeReport(req, res, next)
);

// Get recent executions
router.get('/executions',
  requireRoles('admin', 'manager', 'accountant', 'super_admin'),
  (req, res, next) => reportsController.findRecentExecutions(req, res, next)
);

// Get specific execution
router.get('/executions/:id',
  requireRoles('admin', 'manager', 'accountant', 'super_admin'),
  (req, res, next) => reportsController.findExecutionById(req, res, next)
);

// ============================================================================
// SCHEDULES
// ============================================================================

// List schedules
router.get('/schedules',
  requireRoles('admin', 'manager', 'super_admin'),
  (req, res, next) => reportsController.findAllSchedules(req, res, next)
);

// Create schedule
router.post('/schedules',
  requireRoles('admin', 'super_admin'),
  (req, res, next) => reportsController.createSchedule(req, res, next)
);

// Toggle schedule
router.patch('/schedules/:id/toggle',
  requireRoles('admin', 'super_admin'),
  (req, res, next) => reportsController.toggleSchedule(req, res, next)
);

// Delete schedule
router.delete('/schedules/:id',
  requireRoles('admin', 'super_admin'),
  (req, res, next) => reportsController.deleteSchedule(req, res, next)
);

export default router;
