import { NextResponse } from 'next/server'

export async function GET() {
    try {
        const strapiBaseUrl = process.env.NEXT_PUBLIC_STRAPI_BASE_URL || process.env.NEW_STRAPI_URL || "http://160.187.167.213"
        const baseUrl = strapiBaseUrl.replace(/\/+$/, "")
        
        // Fetch all footer data in parallel
        const [
            quickLinksRes,
            resourcesRes,
            contactRes,
            logoTagRes,
            privacyRes,
            socialIconsRes,
            logoRes
        ] = await Promise.all([
            fetch(`${baseUrl}/api/footer-quick-links`, {
                headers: { Accept: "application/json" },
                next: { revalidate: 300 }
            }),
            fetch(`${baseUrl}/api/footer-resources-links`, {
                headers: { Accept: "application/json" },
                next: { revalidate: 300 }
            }),
            fetch(`${baseUrl}/api/footer-contact-links`, {
                headers: { Accept: "application/json" },
                next: { revalidate: 300 }
            }),
            fetch(`${baseUrl}/api/footer-logo-tag`, {
                headers: { Accept: "application/json" },
                next: { revalidate: 300 }
            }),
            fetch(`${baseUrl}/api/footer-privacy-policy`, {
                headers: { Accept: "application/json" },
                next: { revalidate: 300 }
            }),
            fetch(`${baseUrl}/api/footer-company-svgs`, {
                headers: { Accept: "application/json" },
                next: { revalidate: 300 }
            }),
            fetch(`${baseUrl}/api/logo?populate=*`, {
                headers: { Accept: "application/json" },
                next: { revalidate: 300 }
            })
        ])

        const data = {
            quickLinks: quickLinksRes.ok ? await quickLinksRes.json() : null,
            resources: resourcesRes.ok ? await resourcesRes.json() : null,
            contact: contactRes.ok ? await contactRes.json() : null,
            logoTag: logoTagRes.ok ? await logoTagRes.json() : null,
            privacy: privacyRes.ok ? await privacyRes.json() : null,
            socialIcons: socialIconsRes.ok ? await socialIconsRes.json() : null,
            logo: logoRes.ok ? await logoRes.json() : null,
        }

        return NextResponse.json(data, {
            headers: {
                'Cache-Control': 'public, s-maxage=300, stale-while-revalidate=600',
            },
        })
    } catch (error) {
        console.error('Error fetching footer data:', error)
        return NextResponse.json({ error: 'Failed to fetch footer data' }, { status: 500 })
    }
}

