import { Router } from 'express';
import { DataSource } from 'typeorm';
import {
  ReportDefinition,
  ReportExecution,
  ReportSchedule,
  ReportRecipient,
  ScheduleExecution,
  Dashboard,
  DashboardWidget,
  WidgetQuery,
  CustomReport,
} from './entities';
import {
  ReportsService,
  ReportExecutionService,
  ReportSchedulerService,
  DashboardsService,
  LegacyReportsService,
} from './services';
import { ReportsController, DashboardsController, LegacyReportsController } from './controllers';

/**
 * Creates and configures the reports routes
 *
 * Routes:
 * - /api/v1/reports/definitions/*  - Report definitions CRUD
 * - /api/v1/reports/execute        - Execute a report
 * - /api/v1/reports/executions/*   - Execution history
 * - /api/v1/reports/schedules/*    - Scheduled reports
 * - /api/v1/reports/custom/*       - Custom user reports
 * - /api/v1/dashboards/*           - Dashboards and widgets
 * - /api/v1/reports/legacy/*       - Legacy analytics (sales, inventory, financial)
 *
 * @param dataSource - TypeORM DataSource instance
 * @returns Configured Express Router
 */
export function createReportsRoutes(dataSource: DataSource): Router {
  const router = Router();

  // Initialize repositories
  const definitionRepository = dataSource.getRepository(ReportDefinition);
  const executionRepository = dataSource.getRepository(ReportExecution);
  const scheduleRepository = dataSource.getRepository(ReportSchedule);
  const recipientRepository = dataSource.getRepository(ReportRecipient);
  const scheduleExecutionRepository = dataSource.getRepository(ScheduleExecution);
  const customReportRepository = dataSource.getRepository(CustomReport);
  const dashboardRepository = dataSource.getRepository(Dashboard);
  const widgetRepository = dataSource.getRepository(DashboardWidget);
  const queryRepository = dataSource.getRepository(WidgetQuery);

  // Initialize services
  const reportsService = new ReportsService(
    definitionRepository,
    scheduleRepository,
    recipientRepository,
    customReportRepository
  );

  const executionService = new ReportExecutionService(
    executionRepository,
    definitionRepository,
    dataSource
  );

  const schedulerService = new ReportSchedulerService(
    scheduleRepository,
    scheduleExecutionRepository,
    executionService
  );

  const dashboardsService = new DashboardsService(
    dashboardRepository,
    widgetRepository,
    queryRepository,
    dataSource
  );

  const legacyService = new LegacyReportsService(dataSource);

  // Initialize controllers
  const reportsController = new ReportsController(
    reportsService,
    executionService,
    schedulerService
  );

  const dashboardsController = new DashboardsController(dashboardsService);
  const legacyController = new LegacyReportsController(legacyService);

  // Mount routes
  router.use('/', reportsController.router);
  router.use('/dashboards', dashboardsController.router);
  router.use('/legacy', legacyController.router);

  return router;
}

export default createReportsRoutes;
