// 1Radar — Client-side router (History API, no dependencies)
const Router = (() => {
  const ROUTES = [
    { re: /^\/ads\/([^/]+)$/,          screen: 'explore',    param: 'adId' },
    { re: /^\/ads\/?$/,                 screen: 'explore' },
    { re: /^\/products\/([^/]+)$/,      screen: 'products',   param: 'productId' },
    { re: /^\/products\/?$/,            screen: 'products' },
    { re: /^\/brands\/([^/]+)$/,        screen: 'brands',     param: 'brandId' },
    { re: /^\/brands\/?$/,              screen: 'brands' },
    { re: /^\/saved\/([^/]+)$/,         screen: 'saved',      param: 'folderId' },
    { re: /^\/saved\/?$/,               screen: 'saved' },
    { re: /^\/chat\/([^/]+)$/,          screen: 'chat',       param: 'chatId' },
    { re: /^\/chat\/?$/,                screen: 'chat' },
    { re: /^\/transcript\/([^/]+)$/,    screen: 'transcribe', param: 'transcriptId' },
    { re: /^\/transcript\/?$/,          screen: 'transcribe' },
    { re: /^\/home\/?$/,                screen: 'home' },
    { re: /^\/image\/?$/,               screen: 'image' },
    { re: /^\/workflow\/([^/]+)$/,      screen: 'workflow',   param: 'workflowId' },
    { re: /^\/workflow\/?$/,            screen: 'workflow' },
    { re: /^\/whiteboard\/?$/,          screen: 'whiteboard' },
    { re: /^\/canvas\/?$/,              screen: 'canvas' },
    { re: /^\/video-editor\/?$/,        screen: 'videoeditor' },
    { re: /^\/tools\/?$/,               screen: 'tools' },
    { re: /^\/account\/?$/,             screen: 'account' },
    { re: /^\/shopify\/?$/,             screen: 'shopify' },
    { re: /^\/affiliate\/?$/,           screen: 'affiliate' },
    { re: /^\/game\/?$/,                screen: 'game' },
    { re: /^\/login\/?$/,               screen: 'login' },
    { re: /^\/register\/?$/,            screen: 'register' },
    { re: /^\/?$/,                      screen: 'explore' },
  ];

  function parsePath(pathname) {
    for (const r of ROUTES) {
      const m = pathname.match(r.re);
      if (m) {
        const params = {};
        if (r.param && m[1]) params[r.param] = decodeURIComponent(m[1]);
        return { screen: r.screen, params };
      }
    }
    return { screen: 'explore', params: {} };
  }

  function toPath(screen, params = {}) {
    switch (screen) {
      case 'explore':
        return params.adId ? `/ads/${encodeURIComponent(params.adId)}` : '/ads';
      case 'products':
        return params.productId ? `/products/${encodeURIComponent(params.productId)}` : '/products';
      case 'brands':
        return params.brandId ? `/brands/${encodeURIComponent(params.brandId)}` : '/brands';
      case 'saved':
        return params.folderId && params.folderId !== 'all'
          ? `/saved/${encodeURIComponent(params.folderId)}`
          : '/saved';
      case 'chat':
        return params.chatId ? `/chat/${encodeURIComponent(params.chatId)}` : '/chat';
      case 'transcribe':
        return params.transcriptId
          ? `/transcript/${encodeURIComponent(params.transcriptId)}`
          : '/transcript';
      case 'login':    return '/login';
      case 'register': return '/register';
      case 'workflow':
        return params.workflowId ? `/workflow/${encodeURIComponent(params.workflowId)}` : '/workflow';
      default:
        return `/${screen}`;
    }
  }

  return { parsePath, toPath };
})();

window.Router = Router;
