import 'zone.js/dist/zone-node';

import { APP_BASE_HREF } from '@angular/common';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import * as cors from 'cors';
import { existsSync } from 'fs';
import { join } from 'path';

import { AppServerModule } from './bootstrap.server';

// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
  const server = express();
  const browserBundles = join(process.cwd(), 'dist/apps/<%= appName %>/browser');
  const serverBundles = join(process.cwd(), 'dist/apps/<%= appName %>/server');

  server.use(cors());
  const indexHtml = existsSync(join(browserBundles, 'index.original.html'))
    ? 'index.original.html'
    : 'index';

  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/main/modules/express-engine)
  server.engine(
    'html',
    ngExpressEngine({
      bootstrap: AppServerModule,
    })
  );

  server.set('view engine', 'html');
  server.set('views', browserBundles);


  // Example Express Rest API endpoints
  // server.get('/api/**', (req, res) => { });
  // Serve static files from /browser
  // serve static files
  server.use('/', express.static(browserBundles, { maxAge: '1y' }));
  server.use('/server', express.static(serverBundles, { maxAge: '1y' }));

  // All regular routes use the Universal engine
  server.get('*', (req, res) => {

    res.render(indexHtml, {
      req,
      providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }],
    });
  });

  return server;
}

function run(): void {
  const port = process.env['PORT'] || 4000;

  // Start up the Node server
  const server = app();
  server.listen(port, () => {
    console.log(`Node Express server listening on http://localhost:${port}`);

    /**
     * DO NOT REMOVE IF USING @nrwl/angular:module-federation-dev-ssr executor
     * to serve your Host application with this Remote application.
     * This message allows Nx to determine when the Remote is ready to be
     * consumed by the Host.
     */
    process.send && process.send('nx.server.ready');
  });
}

run();

export * from './bootstrap.server';