import { MetadataRoute } from 'next';
import { blogPosts } from '../lib/blog-data';
import { pillarMeta } from '../lib/pillar-data';
import { authors } from '../lib/author-data';
import { industryPages } from '../lib/industry-pages';

export default function sitemap(): MetadataRoute.Sitemap {
  const baseUrl = 'https://www.qrmaster.net';

  // All free tool slugs
  const freeTools = [
    'url-qr-code',
    'vcard-qr-code',
    'text-qr-code',
    'email-qr-code',
    'sms-qr-code',
    'wifi-qr-code',
    'crypto-qr-code',
    'event-qr-code',
    'facebook-qr-code',
    'instagram-qr-code',
    'twitter-qr-code',
    'youtube-qr-code',
    'whatsapp-qr-code',
    'tiktok-qr-code',
    'geolocation-qr-code',
    'call-qr-code-generator',
    'paypal-qr-code',
    'zoom-qr-code',
    'teams-qr-code',
    'barcode-generator',
  ];

  // All blog posts
  // Filter out future posts so Google doesn't see them
  const blogPages = blogPosts
    .filter(post => {
      const publishDate = post.datePublished ? new Date(post.datePublished) : new Date(post.date);
      return publishDate <= new Date();
    })
    .map((post) => ({
      url: `${baseUrl}/blog/${post.slug}`,
      // Use updatedAt if available, otherwise dateModified or datePublished
      lastModified: post.updatedAt ? new Date(post.updatedAt) : (post.dateModified ? new Date(post.dateModified) : new Date()),
      changeFrequency: 'monthly' as const,
      priority: 0.8,
    }));

  const highPriorityTools: Record<string, number> = {
    'barcode-generator': 0.95,
  };

  const toolPages = freeTools.map((slug) => ({
    url: `${baseUrl}/tools/${slug}`,
    lastModified: new Date(),
    changeFrequency: 'monthly' as const,
    priority: highPriorityTools[slug] ?? 0.8,
  }));

  // Learn hub and pillar pages
  const learnPages = [
    {
      url: `${baseUrl}/learn`,
      lastModified: new Date(),
      changeFrequency: 'weekly' as const,
      priority: 0.9,
    },
    ...pillarMeta.map((pillar) => ({
      url: `${baseUrl}/learn/${pillar.key}`,
      lastModified: new Date(),
      changeFrequency: 'monthly' as const,
      priority: 0.8,
    })),
  ];

  const industryUrls = industryPages.map((industry) => ({
    url: `${baseUrl}/qr-code-for/${industry.slug}`,
    lastModified: new Date(),
    changeFrequency: 'monthly' as const,
    priority: 0.85,
  }));

  const growthUseCasePages = [
    {
      url: `${baseUrl}/use-cases`,
      lastModified: new Date(),
      changeFrequency: 'weekly' as const,
      priority: 0.9,
    },
    {
      url: `${baseUrl}/qr-code-for`,
      lastModified: new Date(),
      changeFrequency: 'weekly' as const,
      priority: 0.9,
    },
    {
      url: `${baseUrl}/use-cases/restaurant-menu-qr-codes`,
      lastModified: new Date(),
      changeFrequency: 'monthly' as const,
      priority: 0.8,
    },
    {
      url: `${baseUrl}/use-cases/business-card-qr-codes`,
      lastModified: new Date(),
      changeFrequency: 'monthly' as const,
      priority: 0.8,
    },
    {
      url: `${baseUrl}/use-cases/event-qr-codes`,
      lastModified: new Date(),
      changeFrequency: 'monthly' as const,
      priority: 0.8,
    },
    {
      url: `${baseUrl}/use-cases/flyer-qr-codes`,
      lastModified: new Date(),
      changeFrequency: 'monthly' as const,
      priority: 0.8,
    },
    {
      url: `${baseUrl}/use-cases/packaging-qr-codes`,
      lastModified: new Date(),
      changeFrequency: 'monthly' as const,
      priority: 0.8,
    },
    {
      url: `${baseUrl}/use-cases/real-estate-sign-qr-codes`,
      lastModified: new Date(),
      changeFrequency: 'monthly' as const,
      priority: 0.8,
    },
    {
      url: `${baseUrl}/use-cases/feedback-qr-codes`,
      lastModified: new Date(),
      changeFrequency: 'monthly' as const,
      priority: 0.8,
    },
    {
      url: `${baseUrl}/use-cases/payment-qr-codes`,
      lastModified: new Date(),
      changeFrequency: 'monthly' as const,
      priority: 0.8,
    },
    {
      url: `${baseUrl}/use-cases/coupon-qr-codes`,
      lastModified: new Date(),
      changeFrequency: 'monthly' as const,
      priority: 0.8,
    },
    {
      url: `${baseUrl}/qr-code-for-marketing-campaigns`,
      lastModified: new Date(),
      changeFrequency: 'monthly' as const,
      priority: 0.85,
    },
  ];

  // Author pages
  const authorPages = authors.map((author) => ({
    url: `${baseUrl}/authors/${author.slug}`,
    lastModified: new Date(),
    changeFrequency: 'monthly' as const,
    priority: 0.7,
  }));

  return [
    {
      url: baseUrl,
      lastModified: new Date(),
      changeFrequency: 'weekly',
      priority: 1.0,
    },
    {
      url: `${baseUrl}/newsletter`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.7,
    },
    {
      url: `${baseUrl}/qr-code-erstellen`,
      lastModified: new Date(),
      changeFrequency: 'weekly',
      priority: 1.0,
    },
    {
      url: `${baseUrl}/qr-code-tracking`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.9,
    },
    {
      url: `${baseUrl}/qr-code-analytics`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.9,
    },
    {
      url: `${baseUrl}/reprint-calculator`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.9,
    },
    {
      url: `${baseUrl}/dynamic-qr-code-generator`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.9,
    },
    {
      url: `${baseUrl}/dynamic-barcode-generator`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.9,
    },
    {
      url: `${baseUrl}/bulk-qr-code-generator`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.9,
    },
    {
      url: `${baseUrl}/custom-qr-code-generator`,
      lastModified: new Date(),
      changeFrequency: 'weekly',
      priority: 0.9,
    },

    {
      url: `${baseUrl}/pricing`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.8,
    },
    {
      url: `${baseUrl}/tools`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.9,
    },
    {
      url: `${baseUrl}/features`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.8,
    },
    {
      url: `${baseUrl}/faq`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.7,
    },
    {
      url: `${baseUrl}/blog`,
      lastModified: new Date(),
      changeFrequency: 'weekly',
      priority: 0.8,
    },

    {
      url: `${baseUrl}/privacy`,
      lastModified: new Date(),
      changeFrequency: 'yearly',
      priority: 0.4,
    },
    {
      url: `${baseUrl}/contact`,
      lastModified: new Date(),
      changeFrequency: 'yearly',
      priority: 0.5,
    },
    {
      url: `${baseUrl}/about`,
      lastModified: new Date(),
      changeFrequency: 'yearly',
      priority: 0.6,
    },
    {
      url: `${baseUrl}/press`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.7,
    },
    {
      url: `${baseUrl}/testimonials`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.7,
    },


    ...toolPages,
    ...blogPages,
    ...learnPages,
    ...growthUseCasePages,
    ...industryUrls,
    ...authorPages,
  ];
}
