import { headers } from 'next/headers';
import cityData from '../data/city.json';
import specialtyData from '../data/specialties.json';
import stateData from '../data/state.json';
import {
    DOMAIN_CONFIG,
    isMainDomain,
    createSitemapUrl,
    createSitemapEntry,
    limitArray,
    getBaseUrl
} from '../utils/sitemap';

// Get current domain from headers
function getCurrentDomain() {
    try {
        const headersList = headers();
        const host = headersList.get('host');
        return host || DOMAIN_CONFIG.MAIN_DOMAIN;
    } catch (error) {
        // Fallback for static generation
        console.warn('Headers not available during static generation, using main domain');
        return DOMAIN_CONFIG.MAIN_DOMAIN;
    }
}

// Get city data based on domain
function getCityByDomain(domain) {
    return cityData.find(city => city.domain === domain);
}

// Get state name by province_id
function getStateById(provinceId) {
    return stateData.find(state => state.id === provinceId)?.name || '';
}

// Generate static pages URLs
function getStaticPages(baseUrl) {
    return [
        {
            url: baseUrl,
            lastModified: new Date(),
            changeFrequency: 'daily',
            priority: 1,
        },
        {
            url: `${baseUrl}/about-us`,
            lastModified: new Date(),
            changeFrequency: 'monthly',
            priority: 0.8,
        },
        {
            url: `${baseUrl}/contact-us`,
            lastModified: new Date(),
            changeFrequency: 'monthly',
            priority: 0.8,
        },
        {
            url: `${baseUrl}/blogs`,
            lastModified: new Date(),
            changeFrequency: 'weekly',
            priority: 0.9,
        },
        {
            url: `${baseUrl}/doctors`,
            lastModified: new Date(),
            changeFrequency: 'daily',
            priority: 0.9,
        },
        {
            url: `${baseUrl}/clinics`,
            lastModified: new Date(),
            changeFrequency: 'daily',
            priority: 0.9,
        },
        {
            url: `${baseUrl}/specialties`,
            lastModified: new Date(),
            changeFrequency: 'weekly',
            priority: 0.9,
        },
    ];
}

// Generate doctor search URLs based on domain
function getDoctorSearchUrls(domain, baseUrl) {
    // Return empty array - no URLs with search parameters
    return [];
}

// Generate clinic search URLs
function getClinicSearchUrls(domain, baseUrl) {
    // Return empty array - no URLs with search parameters
    return [];
}

// Generate specialty pages URLs
function getSpecialtyUrls(domain, baseUrl) {
    // Return empty array - no specialty URLs
    return [];
}

export default async function sitemap() {
    try {
        const domain = getCurrentDomain();
        const baseUrl = getBaseUrl(domain);

        console.log(`Generating sitemap for domain: ${domain}, baseUrl: ${baseUrl}`);
        console.log(`City data length: ${cityData.length}, Specialty data length: ${specialtyData.length}`);

        // Get all URLs
        const staticPages = getStaticPages(baseUrl);
        const doctorSearchUrls = getDoctorSearchUrls(domain, baseUrl);
        const clinicSearchUrls = getClinicSearchUrls(domain, baseUrl);
        const specialtyUrls = getSpecialtyUrls(domain, baseUrl);

        console.log(`Generated URLs - Static: ${staticPages.length}, Doctors: ${doctorSearchUrls.length}, Clinics: ${clinicSearchUrls.length}, Specialties: ${specialtyUrls.length}`);

        // Combine all URLs
        const allUrls = [
            ...staticPages,
            ...doctorSearchUrls,
            ...clinicSearchUrls,
            ...specialtyUrls,
        ];

        // Remove duplicates based on URL
        const uniqueUrls = allUrls.filter((item, index, arr) =>
            arr.findIndex(i => i.url === item.url) === index
        );

        
        return uniqueUrls;
    } catch (error) {
        console.error('Error generating sitemap:', error);
        return [];
    }
}