import { defineRouting } from 'next-intl/routing';

import {
  CREATOR_PAGES_CONFIG,
  CreatorSubpagePathname,
  CreatorSubpagePathnameEn,
} from '@/config/creator-pages/creatorPagesConfig';
import { appLocales, defaultLocale } from '@/config/i18n';

type CategorySlug = {
  hu: string;
  en: string;
};

const CATEGORY_SLUGS: CategorySlug[] = [
  { hu: 'epitomuveszet', en: 'architecture' },
  { hu: 'filmmuveszet', en: 'film' },
  { hu: 'fotomuveszet', en: 'photo' },
  { hu: 'iparmuveszet', en: 'industrial' },
  { hu: 'irodalom', en: 'literature' },
  { hu: 'kepzomuveszet', en: 'fine' },
  { hu: 'muveszetelmelet', en: 'theory' },
  { hu: 'nepmuveszet', en: 'folk' },
  { hu: 'szinhazmuveszet', en: 'theater' },
  { hu: 'zenemuveszet', en: 'music' },
];

type CategoryRoute = {
  huBase: '/alkotok' | '/alkotasok' | '/videok' | '/terkep';
  enBase: '/creators' | '/creations' | '/videos' | '/map';
};

const CATEGORY_ROUTES: CategoryRoute[] = [
  { huBase: '/alkotok', enBase: '/creators' },
  { huBase: '/alkotasok', enBase: '/creations' },
  { huBase: '/videok', enBase: '/videos' },
  { huBase: '/terkep', enBase: '/map' },
];

const CREATOR_SUBPAGES = Object.fromEntries(
  Object.values(CREATOR_PAGES_CONFIG).map(({ route, routeEn }) => [route, { en: routeEn }])
) as Record<CreatorSubpagePathname, { en: CreatorSubpagePathnameEn }>;

function buildCategoryPathnames() {
  const entries: Record<string, { en: string }> = {};

  for (const route of CATEGORY_ROUTES) {
    entries[route.huBase] = { en: route.enBase };

    for (const slug of CATEGORY_SLUGS) {
      entries[`${route.huBase}/${slug.hu}`] = {
        en: `${route.enBase}/${slug.en}`,
      };
    }
  }

  return entries;
}

export const routing = defineRouting({
  locales: appLocales,
  defaultLocale,
  localePrefix: 'as-needed',
  pathnames: {
    ...buildCategoryPathnames(),
    // Home
    '/': '/',
    // Creators
    '/alkotok': {
      en: '/creators',
    },

    // alkotok/[category] is handled by buildCategoryPathnames,

    '/alkotok-aktualis-programokkal': {
      en: '/creators-with-current-programs',
    },
    // --- Creator pages ---
    ...CREATOR_SUBPAGES,
    // root page only redirects for now
    '/alkoto/[id]': {
      en: '/creator/[id]',
    },
    '/alkoto/[id]/kapcsolodasok': {
      en: '/creator/[id]/connections',
    },
    // still redirecting from old timeline route
    '/alkoto/[id]/idovonal': {
      en: '/creator/[id]/timeline',
    },
    '/alkoto/[id]/muveszeti-halo': {
      en: '/creator/[id]/art-network',
    },

    // Creations
    '/alkotasok': {
      en: '/creations',
    },
    '/alkotasok-kepekkel': {
      en: '/creations-with-images',
    },
    // alkotasok/[category] is handled by buildCategoryPathnames,

    '/alkotas/[id]': {
      en: '/creation/[id]',
    },

    // Videos
    '/videok': {
      en: '/videos',
    },
    // videok/[category] is handled by buildCategoryPathnames,

    // Programs
    '/programok': {
      en: '/programs',
    },
    '/programok/[week]': {
      en: '/programs/[week]',
    },
    '/program/[slug]': {
      en: '/program/[slug]',
    },

    // Map
    '/terkep': {
      en: '/map',
    },
    // terkep/[category] is handled by buildCategoryPathnames,

    // Other
    '/kereses': {
      en: '/search',
    },
    '/parhuzamos-eletutak': {
      en: '/parallel-paths',
    },
    '/hirek': {
      en: '/news',
    },
    '/hir/[id]': {
      en: '/news/[id]',
    },
    '/cikk/[slug]': {
      en: '/article/[slug]',
    },

    // Legal pages
    '/adatkezelesi-tajekoztato': {
      en: '/privacy-policy',
    },
    '/sutitajekoztato': {
      en: '/cookie-policy',
    },
    '/szerzoi-jogok': {
      en: '/copyright',
    },
    '/impresszum': {
      en: '/impressum',
    },
    '/projekt-jellemzoi': {
      en: '/project-features',
    },
    '/oldal-mukodese': {
      en: '/site-operation',
    },
  },
});
