import { Hono } from 'hono';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { createObserverRoutes, type ObserverOptions } from './routes.js';
import { createStaticRoutes } from './static.js';

export { createObserverRoutes } from './routes.js';
export { StateAggregator } from './state.js';
export { createAuthMiddleware } from './auth.js';
export { createStaticRoutes } from './static.js';
export type { ObserverOptions } from './routes.js';
export type { ObserverSnapshot } from './state.js';

/**
 * Create a complete Shade Observer Hono app with API + dashboard.
 *
 * Usage:
 * ```ts
 * import { createObserver } from '@shade/observer';
 *
 * const observer = createObserver({
 *   token: process.env.SHADE_OBSERVER_TOKEN!,
 *   clientEvents: sessionManager.getEvents(),
 *   serverEvents: prekeyServerEvents,
 * });
 *
 * // Mount in any Hono app
 * app.route('/shade-observer', observer);
 *
 * // Or run standalone
 * Bun.serve({ port: 3901, fetch: observer.fetch });
 * ```
 */
export function createObserver(
  options: ObserverOptions & { distDir?: string },
): Hono {
  const app = new Hono();
  app.route('/', createObserverRoutes(options));

  const distDir = options.distDir
    ?? join(dirname(fileURLToPath(import.meta.url)), '..', 'dist');
  app.route('/', createStaticRoutes(distDir));

  // Root → dashboard
  app.get('/', (c) => c.redirect('/dashboard/'));

  return app;
}
