import { MetadataRoute } from 'next';

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://gnxsoft.com';
  const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000/api';

  // Static pages
  const staticPages: MetadataRoute.Sitemap = [
    {
      url: baseUrl,
      lastModified: new Date(),
      changeFrequency: 'daily',
      priority: 1.0,
    },
    {
      url: `${baseUrl}/about-us`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.9,
    },
    {
      url: `${baseUrl}/services`,
      lastModified: new Date(),
      changeFrequency: 'weekly',
      priority: 0.9,
    },
    {
      url: `${baseUrl}/case-study`,
      lastModified: new Date(),
      changeFrequency: 'weekly',
      priority: 0.8,
    },
    {
      url: `${baseUrl}/insights`,
      lastModified: new Date(),
      changeFrequency: 'daily',
      priority: 0.8,
    },
    {
      url: `${baseUrl}/career`,
      lastModified: new Date(),
      changeFrequency: 'weekly',
      priority: 0.7,
    },
    {
      url: `${baseUrl}/contact-us`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.8,
    },
    {
      url: `${baseUrl}/support-center`,
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.7,
    },
    {
      url: `${baseUrl}/policy`,
      lastModified: new Date(),
      changeFrequency: 'yearly',
      priority: 0.5,
    },
  ];

  try {
    // Fetch dynamic services
    const servicesResponse = await fetch(`${apiUrl}/services/`, {
      next: { revalidate: 3600 }, // Revalidate every hour
    });
    
    let servicePages: MetadataRoute.Sitemap = [];
    if (servicesResponse.ok) {
      const services = await servicesResponse.json();
      servicePages = services.map((service: any) => ({
        url: `${baseUrl}/services/${service.slug}`,
        lastModified: new Date(service.updated_at || service.created_at),
        changeFrequency: 'weekly' as const,
        priority: service.featured ? 0.9 : 0.7,
      }));
    }

    // Fetch dynamic blog posts
    const blogResponse = await fetch(`${apiUrl}/blog/`, {
      next: { revalidate: 3600 },
    });
    
    let blogPages: MetadataRoute.Sitemap = [];
    if (blogResponse.ok) {
      const posts = await blogResponse.json();
      blogPages = posts.map((post: any) => ({
        url: `${baseUrl}/insights/${post.slug}`,
        lastModified: new Date(post.updated_at || post.published_at),
        changeFrequency: 'weekly' as const,
        priority: 0.7,
      }));
    }

    // Fetch dynamic case studies
    const caseStudiesResponse = await fetch(`${apiUrl}/case-studies/`, {
      next: { revalidate: 3600 },
    });
    
    let caseStudyPages: MetadataRoute.Sitemap = [];
    if (caseStudiesResponse.ok) {
      const caseStudies = await caseStudiesResponse.json();
      caseStudyPages = caseStudies.map((study: any) => ({
        url: `${baseUrl}/case-study/${study.slug}`,
        lastModified: new Date(study.updated_at || study.created_at),
        changeFrequency: 'monthly' as const,
        priority: 0.7,
      }));
    }

    // Fetch dynamic career postings
    const careerResponse = await fetch(`${apiUrl}/career/jobs`, {
      next: { revalidate: 3600 },
    });
    
    let careerPages: MetadataRoute.Sitemap = [];
    if (careerResponse.ok) {
      const positions = await careerResponse.json();
      careerPages = positions.map((position: any) => ({
        url: `${baseUrl}/career/${position.slug}`,
        lastModified: new Date(position.updated_at || position.created_at),
        changeFrequency: 'weekly' as const,
        priority: 0.6,
      }));
    }

    return [...staticPages, ...servicePages, ...blogPages, ...caseStudyPages, ...careerPages];
  } catch (error) {
    // Return at least static pages if API fails
    return staticPages;
  }
}

