import { corsHeaders, json } from './http.js';
import { handleAgentRuntime } from './routes/agentRuntime.js';
import { handleClients, handleClientDetail, handleIpStats } from './routes/clients.js';
import { handleConfigUsage, handleModelUsage } from './routes/configUsage.js';
import { handleGitHubRepoStats } from './routes/githubRepoStats.js';
import { handleHealth } from './routes/health.js';
import { handleLatest } from './routes/latest.js';
import { handleLicenseActivate, handleLicenseConfig, handleOfflineLicense } from './routes/license.js';
import { handleAdminNotice, handlePublicNotice } from './routes/notice.js';
import { handleOverview } from './routes/overview.js';
import { handleProjects } from './routes/projects.js';
import { handleRetention } from './routes/retention.js';
import { handleAdminResources, handlePublicResources, handleResourceImage } from './routes/resources.js';
import { handleTrack } from './routes/track.js';
import { handleTraffic } from './routes/traffic.js';
import {
  OVERVIEW_AI_TOTALS_CRON,
  refreshOverviewAiTotals,
  rollupYesterdayCronStage,
} from './services/analyticsStatsStore.js';

const routes = new Map([
  ['/health', (request, env) => handleHealth(env)],
  ['/track', handleTrack],
  ['/license/activate', handleLicenseActivate],
  ['/notice', handlePublicNotice],
  ['/resources', handlePublicResources],
  ['/resource-image', handleResourceImage],
  ['/api/projects', handleProjects],
  ['/api/notice', handleAdminNotice],
  ['/api/resources', handleAdminResources],
  ['/api/overview', handleOverview],
  ['/api/clients', handleClients],
  ['/api/client-detail', handleClientDetail],
  ['/api/ip-stats', handleIpStats],
  ['/api/traffic', handleTraffic],
  ['/api/latest', handleLatest],
  ['/api/license-config', handleLicenseConfig],
  ['/api/license/offline', handleOfflineLicense],
  ['/api/retention', handleRetention],
  ['/api/config-usage', handleConfigUsage],
  ['/api/model-usage', handleModelUsage],
  ['/api/agent-runtime', handleAgentRuntime],
  ['/api/github-repo-stats', handleGitHubRepoStats],
]);

export default {
  async fetch(request, env) {
    if (request.method === 'OPTIONS') {
      return new Response(null, { status: 204, headers: corsHeaders });
    }

    const url = new URL(request.url);
    const handler = routes.get(url.pathname);
    if (handler) {
      return handler(request, env, url);
    }

    return json({ code: 404, message: 'not found' }, { status: 404 });
  },

  async scheduled(event, env) {
    const cron = event?.cron || '';
    await rollupYesterdayCronStage(env, cron);
    if (cron === OVERVIEW_AI_TOTALS_CRON) {
      try {
        await refreshOverviewAiTotals(env);
      } catch (error) {
        console.error('[analytics] overview AI totals refresh failed', error?.message || String(error));
      }
    }
  },
};
