import { NextResponse } from 'next/server';
import { productService } from '../../../lib/services/productService';
import { PRODUCTS } from '../../../lib/data/products';
export const dynamic = 'force-dynamic';
export const revalidate = 3600; // Revalidate every hour
export async function GET() {
const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://canina.ir';
let productsList: any[] = [];
try {
const res = await productService.getProducts({ limit: 999 });
productsList = res.data && res.data.length > 0 ? res.data : PRODUCTS;
} catch {
productsList = PRODUCTS;
}
// Only active, indexable products
const indexableProducts = productsList.filter(p => !p.noIndex && p.stockStatus !== 'DISCONTINUED');
const itemsXml = indexableProducts.map(product => {
const slug = product.slug || product.artNo || product.id;
const link = `${baseUrl}/shop/${slug}`;
const id = product.artNo || product.id;
const title = product.nameEn ? `${product.nameFa || product.name} (${product.nameEn})` : (product.nameFa || product.name);
const description = product.metaDescription || product.shortDescription || product.description || 'مکمل درمانی اصل کنینا آلمان';
const primaryImg = product.ogImage || product.image;
const imageLink = primaryImg ? (primaryImg.startsWith('http') ? primaryImg : `${baseUrl}${primaryImg}`) : '';
let availability = 'in_stock';
if (product.stockStatus === 'OUT_OF_STOCK') availability = 'out_of_stock';
if (product.stockStatus === 'PRE_ORDER') availability = 'preorder';
const price = product.priceValue ? `${product.priceValue * 10} IRR` : '0 IRR';
const gtinTag = product.barcode ? `${product.barcode}` : '';
const additionalImages = (product.images || [])
.filter((img: string) => img && img !== primaryImg)
.slice(0, 5)
.map((img: string) => `${img.startsWith('http') ? img : `${baseUrl}${img}`}`)
.join('\n ');
return ` -
${id}
${link}
${imageLink}
${additionalImages}
${availability}
${price}
Canina pharma GmbH
${product.artNo}
${gtinTag}
new
`;
}).join('\n');
const xmlContent = `
فید محصولات و مکملهای تخصصی کنینا ایران
${baseUrl}/shop
فید رسمی محصولات و مکملهای دارویی سگ و گربه برند Canina آلمان
${new Date().toUTCString()}
${itemsXml}
`;
return new NextResponse(xmlContent, {
status: 200,
headers: {
'Content-Type': 'application/xml; charset=utf-8',
'Cache-Control': 'public, max-age=3600, s-maxage=3600, stale-while-revalidate=86400',
},
});
}