import { CLUB_MENU_SECTIONS } from './clubWorkspace.js';

export const PRODUCT_TRAINER = 'trainer';
export const PRODUCT_CLUB = 'club';
export const PRODUCT_PLAYER = 'player';

export const FULL_APP_PRODUCTS = [PRODUCT_TRAINER, PRODUCT_CLUB];

const PRODUCT_HOSTS = {
  [PRODUCT_TRAINER]: ['tt-tagebuch.de', 'www.tt-tagebuch.de', 'trainer.localhost'],
  [PRODUCT_CLUB]: ['tt-verein.de', 'www.tt-verein.de', 'club.localhost'],
  [PRODUCT_PLAYER]: ['mein-tt.de', 'www.mein-tt.de', 'player.localhost'],
};

const PRODUCT_CONFIGS = {
  [PRODUCT_TRAINER]: {
    id: PRODUCT_TRAINER,
    brandName: 'Trainings-Tagebuch',
    appName: 'tt-tagebuch.de',
    defaultHomeRoute: '/',
    canonicalUrl: import.meta.env.VITE_CANONICAL_TRAINER_URL || 'https://tt-tagebuch.de',
    seo: {
      siteName: 'Trainings-Tagebuch',
      defaultTitle: 'Trainings-Tagebuch – Tischtennis-Trainingsverwaltung',
      defaultDescription:
        'Trainer ist die bisherige vollständige Tischtennis-Anwendung für Training, Organisation, Turniere, Teams und Vereinsalltag.',
      imagePath: '/android-chrome-512x512.png',
    },
  },
  [PRODUCT_CLUB]: {
    id: PRODUCT_CLUB,
    brandName: 'TT Verein',
    appName: 'tt-verein.de',
    defaultHomeRoute: '/',
    canonicalUrl: import.meta.env.VITE_CANONICAL_CLUB_URL || 'https://tt-verein.de',
    seo: {
      siteName: 'TT Verein',
      defaultTitle: 'TT Verein – Vereinsverwaltung für Tischtennis',
      defaultDescription:
        'TT Verein ist die zentrale Arbeitsplattform für kleine und mittlere Tischtennisvereine mit Fokus auf Mitglieder, Kommunikation, Dokumente, Termine und Vereinsbetrieb.',
      imagePath: '/android-chrome-512x512.png',
    },
  },
  [PRODUCT_PLAYER]: {
    id: PRODUCT_PLAYER,
    brandName: 'Mein TT',
    appName: 'mein-tt.de',
    defaultHomeRoute: '/',
    canonicalUrl: import.meta.env.VITE_CANONICAL_PLAYER_URL || 'https://mein-tt.de',
    seo: {
      siteName: 'Mein TT',
      defaultTitle: 'Mein TT – Tischtennis für Spieler',
      defaultDescription:
        'Mein TT ist die persönliche Tischtennisoberfläche für Spieler mit Kalender, Kontoverknüpfungen, Bestellungen und persönlichen Einstellungen.',
      imagePath: '/android-chrome-512x512.png',
    },
  },
};

export const SIDEBAR_NAVIGATION = {
  [PRODUCT_TRAINER]: [
    {
      id: 'daily-business',
      titleKey: 'navigation.dailyBusiness',
      items: [
        { to: '/members', icon: '👥', labelKey: 'navigation.members', permission: ['members', 'read'] },
        { to: '/diary', icon: '📝', labelKey: 'navigation.diary', permission: ['diary', 'read'] },
        {
          to: '/calendar',
          icon: '📆',
          label: 'Kalender',
          anyPermission: [
            ['diary', 'read'],
            ['schedule', 'read'],
            ['tournaments', 'read'],
          ],
        },
        { to: '/pending-approvals', icon: '⏳', labelKey: 'navigation.approvals', capability: 'approvals' },
        { to: '/training-stats', icon: '📊', labelKey: 'navigation.statistics', permission: ['statistics', 'read'] },
      ],
    },
    {
      id: 'competitions',
      titleKey: 'navigation.competitions',
      items: [
        { to: '/tournaments', icon: '🏆', labelKey: 'navigation.clubTournaments', permission: ['tournaments', 'read'] },
        { to: '/tournament-participations', icon: '📋', labelKey: 'navigation.tournamentParticipations', permission: ['tournaments', 'read'] },
        { to: '/schedule', icon: '📅', labelKey: 'navigation.schedule', permission: ['schedule', 'read'] },
        { to: '/friendly-matches', icon: '🤝', label: 'Freundschaftsspiele', permission: ['schedule', 'read'] },
      ],
    },
    {
      id: 'settings',
      titleKey: 'navigation.settings',
      items: [
        { to: '/club-settings', icon: '🏛️', labelKey: 'navigation.clubSettings', capability: 'admin' },
        { to: '/predefined-activities', icon: '🎯', labelKey: 'navigation.predefinedActivities', permission: ['predefined_activities', 'read'] },
        { to: '/team-management', icon: '🧩', labelKey: 'navigation.teamManagement', permission: ['teams', 'read'] },
        { to: '/billing', icon: '🧾', labelKey: 'navigation.billing', permission: ['members', 'read'] },
      ],
    },
  ],
  [PRODUCT_CLUB]: CLUB_MENU_SECTIONS,
  [PRODUCT_PLAYER]: [
    {
      id: 'player-area',
      title: 'Mein Bereich',
      items: [
        { to: '/calendar', icon: '📆', label: 'Kalender' },
        { to: '/mytischtennis-account', icon: '🔗', labelKey: 'navigation.myTischtennisAccount' },
        { to: '/clicktt-account', icon: '🏓', labelKey: 'navigation.clickTtAccount' },
        { to: '/orders', icon: '📦', labelKey: 'navigation.orders' },
        { to: '/personal-settings', icon: '⚙️', labelKey: 'navigation.personalSettings' },
      ],
    },
  ],
};

export function normalizeProduct(product) {
  if (product === PRODUCT_PLAYER) return PRODUCT_PLAYER;
  if (product === PRODUCT_CLUB) return PRODUCT_CLUB;
  return PRODUCT_TRAINER;
}

export function resolveProductFromHostname(hostname = '') {
  const normalizedHostname = String(hostname || '').toLowerCase();

  if (PRODUCT_HOSTS[PRODUCT_TRAINER].includes(normalizedHostname)) {
    return PRODUCT_TRAINER;
  }

  if (PRODUCT_HOSTS[PRODUCT_PLAYER].includes(normalizedHostname)) {
    return PRODUCT_PLAYER;
  }

  if (PRODUCT_HOSTS[PRODUCT_CLUB].includes(normalizedHostname)) {
    return PRODUCT_CLUB;
  }

  return PRODUCT_TRAINER;
}

export function resolveCurrentProduct() {
  const override = normalizeProduct(import.meta.env.VITE_APP_PRODUCT);
  if (import.meta.env.VITE_APP_PRODUCT) {
    return override;
  }

  if (typeof window === 'undefined') {
    return PRODUCT_TRAINER;
  }

  return resolveProductFromHostname(window.location.hostname);
}

export function getProductConfig(product = resolveCurrentProduct()) {
  return PRODUCT_CONFIGS[normalizeProduct(product)];
}

export function getDefaultHomeRoute(product = resolveCurrentProduct()) {
  return getProductConfig(product).defaultHomeRoute;
}

export function isProductRouteAllowed(product, routeProducts = []) {
  if (!Array.isArray(routeProducts) || routeProducts.length === 0) {
    return true;
  }

  return routeProducts.includes(normalizeProduct(product));
}

export function getCurrentBrandName() {
  return getProductConfig(resolveCurrentProduct()).brandName;
}
