'use client';
import { useRouter, usePathname } from 'next/navigation';
import { useCallback, useMemo } from 'react';

export type RouteName =
  | 'home'
  | 'search'
  | 'category'
  | 'detail'
  | 'booking'
  | 'auth'
  | 'pricing'
  | 'annonces'
  | 'annonce'
  | 'post'
  | 'compte'
  | 'pro'
  | 'vendeur'
  | 'apropos'
  | 'aide'
  | 'villes'
  | 'forgot'
  | 'reset';

export type GoParams = Record<string, string | number | undefined>;

export const PUBLIC_CHROME: RouteName[] = [
  'home',
  'search',
  'category',
  'detail',
  'booking',
  'pricing',
  'annonces',
  'annonce',
  'post',
  'vendeur',
  'apropos',
  'aide',
  'villes',
];

function buildQuery(params: GoParams, drop: string[] = []): string {
  const u = new URLSearchParams();
  for (const [k, v] of Object.entries(params)) {
    if (drop.includes(k)) continue;
    if (v === undefined || v === null || v === '') continue;
    u.set(k, String(v));
  }
  const s = u.toString();
  return s ? `?${s}` : '';
}

export function buildHref(name: RouteName, params: GoParams = {}): string {
  switch (name) {
    case 'home':
      return '/';
    case 'search':
      return `/explorer${buildQuery(params)}`;
    case 'category':
      return `/explorer${buildQuery(params)}`;
    case 'detail':
      return `/commerce/${params.id ?? 1}${buildQuery(params, ['id'])}`;
    case 'booking':
      return `/reserver/${params.id ?? 1}${buildQuery(params, ['id'])}`;
    case 'auth':
      return `/connexion${buildQuery(params)}`;
    case 'pricing':
      return '/tarifs';
    case 'annonces':
      return `/annonces${buildQuery(params)}`;
    case 'annonce':
      return `/annonces/${params.id ?? 101}${buildQuery(params, ['id'])}`;
    case 'post':
      return `/annonces/publier${buildQuery(params)}`;
    case 'compte':
      return `/compte${buildQuery(params)}`;
    case 'pro':
      return '/pro';
    case 'vendeur':
      return `/vendeur/${params.id ?? 1}${buildQuery(params, ['id'])}`;
    case 'apropos':
      return '/a-propos';
    case 'aide':
      return '/aide';
    case 'villes':
      return '/villes';
    case 'forgot':
      return '/mot-de-passe-oublie';
    case 'reset':
      return '/reinitialiser';
  }
}

export function routeNameFromPath(pathname: string | null): RouteName {
  if (!pathname || pathname === '/') return 'home';
  if (pathname.startsWith('/explorer')) return 'search';
  if (pathname.startsWith('/commerce')) return 'detail';
  if (pathname.startsWith('/reserver')) return 'booking';
  if (pathname.startsWith('/connexion')) return 'auth';
  if (pathname === '/tarifs') return 'pricing';
  if (pathname === '/annonces/publier') return 'post';
  if (pathname.startsWith('/annonces/')) return 'annonce';
  if (pathname.startsWith('/annonces')) return 'annonces';
  if (pathname.startsWith('/compte')) return 'compte';
  if (pathname.startsWith('/pro')) return 'pro';
  if (pathname.startsWith('/vendeur')) return 'vendeur';
  if (pathname === '/a-propos') return 'apropos';
  if (pathname === '/aide') return 'aide';
  if (pathname === '/villes') return 'villes';
  if (pathname === '/mot-de-passe-oublie') return 'forgot';
  if (pathname === '/reinitialiser') return 'reset';
  return 'home';
}

export function useGo() {
  const router = useRouter();
  const pathname = usePathname();

  const go = useCallback(
    (name: RouteName, params: GoParams = {}) => {
      router.push(buildHref(name, params));
    },
    [router]
  );

  const route = useMemo(() => ({ name: routeNameFromPath(pathname) }), [pathname]);
  return { go, route };
}
