import { SERVICES, LOCATIONS, LANGS, SITE } from "@/lib/config";
import { getAllPosts, getPaginatedPosts } from "@/lib/blog";

export default async function sitemap() {
  const base = `https://${SITE.domain}`;
  const urls: Array<{ url: string; changefreq: string; priority: number }> = [];

  for (const lang of LANGS) {
    // Main pages
    urls.push({ url: `${base}/${lang}`, changefreq: "weekly", priority: 1.0 });
    urls.push({
      url: `${base}/${lang}/services`,
      changefreq: "weekly",
      priority: 0.8,
    });
    urls.push({
      url: `${base}/${lang}/locations`,
      changefreq: "weekly",
      priority: 0.8,
    });
    urls.push({
      url: `${base}/${lang}/pricing`,
      changefreq: "monthly",
      priority: 0.7,
    });
    urls.push({
      url: `${base}/${lang}/contact`,
      changefreq: "monthly",
      priority: 0.7,
    });
    urls.push({
      url: `${base}/${lang}/careers`,
      changefreq: "monthly",
      priority: 0.7,
    });

    // SEO & Lead Gen Pages
    urls.push({
      url: `${base}/${lang}/why-automation`,
      changefreq: "monthly",
      priority: 0.9,
    });
    urls.push({
      url: `${base}/${lang}/comparison`,
      changefreq: "monthly",
      priority: 0.8,
    });
    urls.push({
      url: `${base}/${lang}/glossary`,
      changefreq: "monthly",
      priority: 0.7,
    });
    urls.push({
      url: `${base}/${lang}/sitemap`,
      changefreq: "monthly",
      priority: 0.5,
    });

    // Industry pages
    const industryPages = [
      "industries",
      "industries/health-care",
      "industries/health-care/telehealth",
      "industries/health-care/labs-diagnostics",
      "industries/beauty-wellness",
      "industries/education",
      "industries/sports-academies",
      "industries/retail-services",
      "industries/food-leisure",
      "industries/food-hospitality-stays",
      "industries/food-hospitality-stays/restaurants-cafes",
      "industries/food-hospitality-stays/hotels-accommodation",
      "industries/events-venues",
      "industries/property-facilities",
      "industries/hotels-accommodation",
      "industries/home-services-trades",
      "industries/pet-services",
      "industries/public-sector",
      "industries/nonprofit-community",
      "industries/mobility-industry",
      "industries/mobility-industry/mobility-automotive",
      "industries/vehicle-rental-chauffeur",
    ];

    for (const page of industryPages) {
      urls.push({
        url: `${base}/${lang}/${page}`,
        changefreq: "weekly",
        priority: 0.8,
      });
    }

    // Blog main page
    urls.push({
      url: `${base}/${lang}/blog`,
      changefreq: "daily",
      priority: 0.8,
    });

    // Blog posts
    const posts = await getAllPosts(lang, true);
    for (const post of posts) {
      urls.push({
        url: `${base}/${lang}/blog/${post.slug}`,
        changefreq: "monthly",
        priority: 0.6,
      });
    }

    // Blog pagination pages
    const { totalPages } = await getPaginatedPosts(lang, 1, 9);
    for (let page = 2; page <= totalPages; page++) {
      urls.push({
        url: `${base}/${lang}/blog/page/${page}`,
        changefreq: "daily",
        priority: 0.5,
      });
    }

    // Service pages
    for (const s of Object.values(SERVICES)) {
      urls.push({
        url: `${base}/${lang}/services/${s.slug[lang]}`,
        changefreq: "monthly",
        priority: 0.7,
      });
    }

    // Location pages
    for (const l of Object.values(LOCATIONS)) {
      urls.push({
        url: `${base}/${lang}/locations/${l.slug[lang]}`,
        changefreq: "monthly",
        priority: 0.7,
      });
      for (const s of Object.values(SERVICES)) {
        urls.push({
          url: `${base}/${lang}/${l.slug[lang]}/${s.slug[lang]}`,
          changefreq: "monthly",
          priority: 0.6,
        });
      }
    }
  }

  return urls;
}
