import consola from "consola";
import "reflect-metadata";
import { createExpressServer } from "routing-controllers";
import { config, e as errors } from './config';
import loaders from "./loaders/index";
import authchecker from "./middlewares/authchecker";
import { ErrorHandler } from './middlewares/ErrorHandler';
import UserChecker from './middlewares/UserChecker';

// Import all controllers directly to avoid Bun + routing-controllers glob/require issues
import { AuthController } from './controllers/AuthController';
import { DonationController } from './controllers/DonationController';
import { DonorController } from './controllers/DonorController';
import { GroupContactController } from './controllers/GroupContactController';
import { ImportController } from './controllers/ImportController';
import { MeController } from './controllers/MeController';
import { PermissionController } from './controllers/PermissionController';
import { RunnerCardController } from './controllers/RunnerCardController';
import { RunnerController } from './controllers/RunnerController';
import { RunnerOrganizationController } from './controllers/RunnerOrganizationController';
import { RunnerSelfServiceController } from './controllers/RunnerSelfServiceController';
import { RunnerTeamController } from './controllers/RunnerTeamController';
import { ScanController } from './controllers/ScanController';
import { ScanStationController } from './controllers/ScanStationController';
import { StatsClientController } from './controllers/StatsClientController';
import { StatsController } from './controllers/StatsController';
import { StatusController } from './controllers/StatusController';
import { TrackController } from './controllers/TrackController';
import { UserController } from './controllers/UserController';
import { UserGroupController } from './controllers/UserGroupController';

const app = createExpressServer({
  authorizationChecker: authchecker,
  currentUserChecker: UserChecker,
  middlewares: [ErrorHandler],
  development: config.development,
  cors: true,
  routePrefix: "/api",
  controllers: [
    AuthController,
    DonationController,
    DonorController,
    GroupContactController,
    ImportController,
    MeController,
    PermissionController,
    RunnerCardController,
    RunnerController,
    RunnerOrganizationController,
    RunnerSelfServiceController,
    RunnerTeamController,
    ScanController,
    ScanStationController,
    StatsClientController,
    StatsController,
    StatusController,
    TrackController,
    UserController,
    UserGroupController,
  ],
});

async function main() {
  await loaders(app);
  if (config.testing) {
    consola.info("🛠[config]: Discovered testing env. Mailing errors will get ignored!")
  }
  app.listen(config.internal_port, () => {
    consola.success(
      `⚡️[server]: Server is running at http://localhost:${config.internal_port}`
    );
  });
}
if (errors === 0) {
  main();
} else {
  consola.error("error");
  // something's wrong
}
