import { Router } from 'express';
import { financialController } from './financial.controller.js';
import { authenticate } from '../../shared/middleware/auth.middleware.js';
import { requireAccess } from '../../shared/middleware/rbac.middleware.js';

const router = Router();

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

// ========== ACCOUNT TYPES ==========
router.get('/account-types', (req, res, next) => financialController.getAccountTypes(req, res, next));

// ========== ACCOUNTS ==========
router.get('/accounts', requireAccess({ roles: ['admin', 'accountant', 'manager'], permission: 'accounts:read' }), (req, res, next) =>
  financialController.getAccounts(req, res, next)
);
router.get('/accounts/:id', requireAccess({ roles: ['admin', 'accountant', 'manager'], permission: 'accounts:read' }), (req, res, next) =>
  financialController.getAccount(req, res, next)
);
router.get('/accounts/:id/balance', requireAccess({ roles: ['admin', 'accountant', 'manager'], permission: 'accounts:read' }), (req, res, next) =>
  financialController.getAccountBalance(req, res, next)
);
router.post('/accounts', requireAccess({ roles: ['admin', 'accountant'], permission: 'accounts:create' }), (req, res, next) =>
  financialController.createAccount(req, res, next)
);
router.put('/accounts/:id', requireAccess({ roles: ['admin', 'accountant'], permission: 'accounts:update' }), (req, res, next) =>
  financialController.updateAccount(req, res, next)
);
router.delete('/accounts/:id', requireAccess({ roles: ['admin'], permission: 'accounts:delete' }), (req, res, next) =>
  financialController.deleteAccount(req, res, next)
);

// ========== JOURNALS ==========
router.get('/journals', requireAccess({ roles: ['admin', 'accountant', 'manager'], permission: 'journal_entries:read' }), (req, res, next) =>
  financialController.getJournals(req, res, next)
);
router.get('/journals/:id', requireAccess({ roles: ['admin', 'accountant', 'manager'], permission: 'journal_entries:read' }), (req, res, next) =>
  financialController.getJournal(req, res, next)
);
router.post('/journals', requireAccess({ roles: ['admin'], permission: 'journal_entries:create' }), (req, res, next) =>
  financialController.createJournal(req, res, next)
);
router.put('/journals/:id', requireAccess({ roles: ['admin'], permission: 'journal_entries:update' }), (req, res, next) =>
  financialController.updateJournal(req, res, next)
);
router.delete('/journals/:id', requireAccess({ roles: ['admin'], permission: 'journal_entries:delete' }), (req, res, next) =>
  financialController.deleteJournal(req, res, next)
);

// ========== JOURNAL ENTRIES ==========
router.get('/entries', requireAccess({ roles: ['admin', 'accountant', 'manager'], permission: 'journal_entries:read' }), (req, res, next) =>
  financialController.getJournalEntries(req, res, next)
);
router.get('/entries/:id', requireAccess({ roles: ['admin', 'accountant', 'manager'], permission: 'journal_entries:read' }), (req, res, next) =>
  financialController.getJournalEntry(req, res, next)
);
router.post('/entries', requireAccess({ roles: ['admin', 'accountant'], permission: 'journal_entries:create' }), (req, res, next) =>
  financialController.createJournalEntry(req, res, next)
);
router.put('/entries/:id', requireAccess({ roles: ['admin', 'accountant'], permission: 'journal_entries:update' }), (req, res, next) =>
  financialController.updateJournalEntry(req, res, next)
);
router.post('/entries/:id/post', requireAccess({ roles: ['admin', 'accountant'], permission: 'journal_entries:post' }), (req, res, next) =>
  financialController.postJournalEntry(req, res, next)
);
router.post('/entries/:id/cancel', requireAccess({ roles: ['admin'], permission: 'journal_entries:reverse' }), (req, res, next) =>
  financialController.cancelJournalEntry(req, res, next)
);
router.delete('/entries/:id', requireAccess({ roles: ['admin'], permission: 'journal_entries:delete' }), (req, res, next) =>
  financialController.deleteJournalEntry(req, res, next)
);

// ========== INVOICES ==========
router.get('/invoices', requireAccess({ roles: ['admin', 'accountant', 'manager', 'sales'], permission: 'invoices:read' }), (req, res, next) =>
  financialController.getInvoices(req, res, next)
);
router.get('/invoices/:id', requireAccess({ roles: ['admin', 'accountant', 'manager', 'sales'], permission: 'invoices:read' }), (req, res, next) =>
  financialController.getInvoice(req, res, next)
);
router.post('/invoices', requireAccess({ roles: ['admin', 'accountant', 'sales'], permission: 'invoices:create' }), (req, res, next) =>
  financialController.createInvoice(req, res, next)
);
router.put('/invoices/:id', requireAccess({ roles: ['admin', 'accountant', 'sales'], permission: 'invoices:update' }), (req, res, next) =>
  financialController.updateInvoice(req, res, next)
);
router.post('/invoices/:id/validate', requireAccess({ roles: ['admin', 'accountant'], permission: 'invoices:validate' }), (req, res, next) =>
  financialController.validateInvoice(req, res, next)
);
router.post('/invoices/:id/cancel', requireAccess({ roles: ['admin', 'accountant'], permission: 'invoices:cancel' }), (req, res, next) =>
  financialController.cancelInvoice(req, res, next)
);
router.delete('/invoices/:id', requireAccess({ roles: ['admin'], permission: 'invoices:delete' }), (req, res, next) =>
  financialController.deleteInvoice(req, res, next)
);

// Invoice lines
router.post('/invoices/:id/lines', requireAccess({ roles: ['admin', 'accountant', 'sales'], permission: 'invoices:update' }), (req, res, next) =>
  financialController.addInvoiceLine(req, res, next)
);
router.put('/invoices/:id/lines/:lineId', requireAccess({ roles: ['admin', 'accountant', 'sales'], permission: 'invoices:update' }), (req, res, next) =>
  financialController.updateInvoiceLine(req, res, next)
);
router.delete('/invoices/:id/lines/:lineId', requireAccess({ roles: ['admin', 'accountant', 'sales'], permission: 'invoices:update' }), (req, res, next) =>
  financialController.removeInvoiceLine(req, res, next)
);

// ========== PAYMENTS ==========
router.get('/payments', requireAccess({ roles: ['admin', 'accountant', 'manager'], permission: 'payments:read' }), (req, res, next) =>
  financialController.getPayments(req, res, next)
);
router.get('/payments/:id', requireAccess({ roles: ['admin', 'accountant', 'manager'], permission: 'payments:read' }), (req, res, next) =>
  financialController.getPayment(req, res, next)
);
router.post('/payments', requireAccess({ roles: ['admin', 'accountant'], permission: 'payments:create' }), (req, res, next) =>
  financialController.createPayment(req, res, next)
);
router.put('/payments/:id', requireAccess({ roles: ['admin', 'accountant'], permission: 'payments:update' }), (req, res, next) =>
  financialController.updatePayment(req, res, next)
);
router.post('/payments/:id/post', requireAccess({ roles: ['admin', 'accountant'], permission: 'payments:apply' }), (req, res, next) =>
  financialController.postPayment(req, res, next)
);
router.post('/payments/:id/reconcile', requireAccess({ roles: ['admin', 'accountant'], permission: 'bank_reconciliation:update' }), (req, res, next) =>
  financialController.reconcilePayment(req, res, next)
);
router.post('/payments/:id/cancel', requireAccess({ roles: ['admin', 'accountant'], permission: 'payments:delete' }), (req, res, next) =>
  financialController.cancelPayment(req, res, next)
);
router.delete('/payments/:id', requireAccess({ roles: ['admin'], permission: 'payments:delete' }), (req, res, next) =>
  financialController.deletePayment(req, res, next)
);

// ========== TAXES ==========
router.get('/taxes', requireAccess({ roles: ['admin', 'accountant', 'manager', 'sales'], permission: 'accounts:read' }), (req, res, next) =>
  financialController.getTaxes(req, res, next)
);
router.get('/taxes/:id', requireAccess({ roles: ['admin', 'accountant', 'manager', 'sales'], permission: 'accounts:read' }), (req, res, next) =>
  financialController.getTax(req, res, next)
);
router.post('/taxes', requireAccess({ roles: ['admin', 'accountant'], permission: 'accounts:create' }), (req, res, next) =>
  financialController.createTax(req, res, next)
);
router.put('/taxes/:id', requireAccess({ roles: ['admin', 'accountant'], permission: 'accounts:update' }), (req, res, next) =>
  financialController.updateTax(req, res, next)
);
router.delete('/taxes/:id', requireAccess({ roles: ['admin'], permission: 'accounts:delete' }), (req, res, next) =>
  financialController.deleteTax(req, res, next)
);

export default router;
