// IMPORTANT: DOM polyfill must be imported FIRST, before any browser-dependent libraries
import './src/ssr-dom-polyfill';

import { APP_BASE_HREF } from '@angular/common';
import { AngularNodeAppEngine, createNodeRequestHandler, writeResponseToNodeResponse } from '@angular/ssr/node';
import { ɵsetAngularAppEngineManifest as setAngularAppEngineManifest } from '@angular/ssr';
import express from 'express';
import { fileURLToPath } from 'node:url';
import { dirname, join, resolve } from 'node:path';

// The Express app is exported so that it can be used by serverless Functions.
export async function app(): Promise<express.Express> {
  const server = express();
  const serverDistFolder = dirname(fileURLToPath(import.meta.url));
  const browserDistFolder = resolve(serverDistFolder, '../browser');
  const indexHtml = join(serverDistFolder, 'index.server.html');

  // Explicitly load and set the Angular app engine manifest
  // This is required for environments where the manifest is not auto-loaded
  const manifestPath = join(serverDistFolder, 'angular-app-engine-manifest.mjs');
  const manifest = await import(manifestPath);
  setAngularAppEngineManifest(manifest.default);

  const angularApp = new AngularNodeAppEngine();

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

  // Sitemap XML endpoints - MUST be before static files middleware
  server.get('/sitemap.xml', async (req, res) => {
    try {
      const sitemapIndexXml = `<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>https://www.bizmatch.net/sitemap-static.xml</loc>
    <lastmod>${new Date().toISOString().split('T')[0]}</lastmod>
  </sitemap>
</sitemapindex>`;

      res.header('Content-Type', 'application/xml; charset=utf-8');
      res.send(sitemapIndexXml);
    } catch (error) {
      console.error('[SSR] Error generating sitemap index:', error);
      res.status(500).send('Error generating sitemap');
    }
  });

  server.get('/sitemap-static.xml', async (req, res) => {
    try {
      const sitemapXml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://www.bizmatch.net/</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>https://www.bizmatch.net/home</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>https://www.bizmatch.net/businessListings</loc>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
  </url>
  <url>
    <loc>https://www.bizmatch.net/commercialPropertyListings</loc>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
  </url>
  <url>
    <loc>https://www.bizmatch.net/brokerListings</loc>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
  </url>
  <url>
    <loc>https://www.bizmatch.net/terms-of-use</loc>
    <changefreq>monthly</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc>https://www.bizmatch.net/privacy-statement</loc>
    <changefreq>monthly</changefreq>
    <priority>0.5</priority>
  </url>
</urlset>`;

      res.header('Content-Type', 'application/xml; charset=utf-8');
      res.send(sitemapXml);
    } catch (error) {
      console.error('[SSR] Error generating static sitemap:', error);
      res.status(500).send('Error generating sitemap');
    }
  });

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

  // All regular routes use the Angular engine
  server.get('*', async (req, res, next) => {
    console.log(`[SSR] Handling request: ${req.method} ${req.url}`);
    // Cache SSR-rendered pages at CDN level
    res.setHeader('Cache-Control', 'public, s-maxage=300, stale-while-revalidate=600');
    try {
      const response = await angularApp.handle(req);
      if (response) {
        console.log(`[SSR] Response received for ${req.url}, status: ${response.status}`);
        writeResponseToNodeResponse(response, res);
      } else {
        console.log(`[SSR] No response for ${req.url} - Angular engine returned null`);
        console.log(`[SSR] This usually means the route couldn't be rendered. Check for:
   1. Browser API usage in components
   2. Missing platform checks
   3. Errors during component initialization`);
        res.sendStatus(404);
      }
    } catch (err) {
      console.error(`[SSR] Error handling ${req.url}:`, err);
      console.error(`[SSR] Stack trace:`, err.stack);
      next(err);
    }
  });

  return server;
}

// Global error handlers for debugging
process.on('unhandledRejection', (reason, promise) => {
  console.error('[SSR] Unhandled Rejection at:', promise, 'reason:', reason);
});

process.on('uncaughtException', (error) => {
  console.error('[SSR] Uncaught Exception:', error);
  console.error('[SSR] Stack:', error.stack);
});

async function run(): Promise<void> {
  const port = process.env['PORT'] || 4200;

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

run();
