import { MetadataRoute } from 'next';
import { locales } from '@/i18n/config';
import { getAllCitySlugs } from '@/data/cities';
import { getAllServiceSlugs } from '@/data/services';

const BASE_URL = process.env.NEXT_PUBLIC_SITE_URL || 'https://damjan-savic.com';

// Blog slugs
const blogSlugs = [
  'n8n-automatisierung-tutorial',
  'geo-generative-engine-optimization',
  'gpt-api-integration-praxisguide',
  'ki-agenten-unternehmen-guide',
  'n8n-make-zapier-vergleich',
  'voice-ai-kundenservice-trends',
  'saas-mvp-entwicklung-4-wochen',
  'erp-integration-breuninger',
];

// Portfolio slugs
const portfolioSlugs = [
  'ai-data-reader',
  'smart-warehouse',
  'website-mit-ki',
  'power-platform-governance',
  'automated-ad-creatives',
  'kamenpro',
  'ai-music-production',
  'cursor-ide',
];

function generateAlternates(path: string) {
  const alternates: { [key: string]: string } = {
    // x-default zeigt auf deutsche Version als Fallback
    'x-default': `${BASE_URL}${path || '/'}`,
  };
  locales.forEach((locale) => {
    const localePath = locale === 'de' ? path : `/${locale}${path}`;
    alternates[locale] = `${BASE_URL}${localePath}`;
  });
  return alternates;
}

export default function sitemap(): MetadataRoute.Sitemap {
  const today = new Date().toISOString().split('T')[0];

  const sitemap: MetadataRoute.Sitemap = [];

  // Static pages
  const staticPages = [
    { path: '', priority: 1.0, changeFrequency: 'weekly' as const },
    { path: '/about', priority: 0.8, changeFrequency: 'monthly' as const },
    { path: '/portfolio', priority: 0.9, changeFrequency: 'weekly' as const },
    { path: '/blog', priority: 0.9, changeFrequency: 'weekly' as const },
    { path: '/contact', priority: 0.8, changeFrequency: 'monthly' as const },
    { path: '/leistungen', priority: 0.9, changeFrequency: 'weekly' as const },
    { path: '/privacy', priority: 0.3, changeFrequency: 'yearly' as const },
    { path: '/terms', priority: 0.3, changeFrequency: 'yearly' as const },
    { path: '/imprint', priority: 0.3, changeFrequency: 'yearly' as const },
  ];

  // Add static pages for all locales
  staticPages.forEach((page) => {
    locales.forEach((locale) => {
      const localePath = locale === 'de' ? page.path : `/${locale}${page.path}`;
      sitemap.push({
        url: `${BASE_URL}${localePath || '/'}`,
        lastModified: today,
        changeFrequency: page.changeFrequency,
        priority: page.priority,
        alternates: {
          languages: generateAlternates(page.path),
        },
      });
    });
  });

  // Add service pages
  const serviceSlugs = getAllServiceSlugs();
  serviceSlugs.forEach((slug) => {
    locales.forEach((locale) => {
      const localePath = locale === 'de' ? `/leistungen/${slug}` : `/${locale}/leistungen/${slug}`;
      sitemap.push({
        url: `${BASE_URL}${localePath}`,
        lastModified: today,
        changeFrequency: 'monthly',
        priority: 0.8,
        alternates: {
          languages: generateAlternates(`/leistungen/${slug}`),
        },
      });
    });
  });

  // Add city pages
  const citySlugs = getAllCitySlugs();
  citySlugs.forEach((slug) => {
    locales.forEach((locale) => {
      const localePath = locale === 'de' ? `/leistungen/standort/${slug}` : `/${locale}/leistungen/standort/${slug}`;
      sitemap.push({
        url: `${BASE_URL}${localePath}`,
        lastModified: today,
        changeFrequency: 'monthly',
        priority: 0.7,
        alternates: {
          languages: generateAlternates(`/leistungen/standort/${slug}`),
        },
      });
    });
  });

  // Add blog posts
  blogSlugs.forEach((slug) => {
    locales.forEach((locale) => {
      const localePath = locale === 'de' ? `/blog/${slug}` : `/${locale}/blog/${slug}`;
      sitemap.push({
        url: `${BASE_URL}${localePath}`,
        lastModified: today,
        changeFrequency: 'monthly',
        priority: 0.7,
        alternates: {
          languages: generateAlternates(`/blog/${slug}`),
        },
      });
    });
  });

  // Add portfolio projects
  portfolioSlugs.forEach((slug) => {
    locales.forEach((locale) => {
      const localePath = locale === 'de' ? `/portfolio/${slug}` : `/${locale}/portfolio/${slug}`;
      sitemap.push({
        url: `${BASE_URL}${localePath}`,
        lastModified: today,
        changeFrequency: 'monthly',
        priority: 0.8,
        alternates: {
          languages: generateAlternates(`/portfolio/${slug}`),
        },
      });
    });
  });

  return sitemap;
}
