import { prisma } from '@/lib/db';
import { NextResponse } from 'next/server';

export const dynamic = 'force-dynamic';

export async function GET() {
  const [settings, services] = await Promise.all([
    prisma.siteSettings.findFirst(),
    prisma.service.findMany({ orderBy: { createdAt: 'asc' } }),
  ]);

  const siteUrl = settings?.siteUrl || 'https://moybeach.com';
  const siteName = 'Moy Beach';

  const serviceList = services.length > 0
    ? services.map(s => `- **${s.title}**: ${s.description}`).join('\n')
    : '- Beach hizmetleri\n- Restoran\n- Otel';

  const contactLines = [
    settings?.phone ? `- Telefon: ${settings.phone}` : '',
    settings?.email ? `- E-posta: ${settings.email}` : '',
    settings?.addressText1 ? `- Adres: ${settings.addressText1}${settings.addressText2 ? ', ' + settings.addressText2 : ''}` : '',
    settings?.workingHours ? `- Çalışma Saatleri: ${settings.workingHours}` : '',
  ].filter(Boolean).join('\n');

  const socialLines = [
    settings?.instagramUrl ? `- Instagram: ${settings.instagramUrl}` : '',
    settings?.facebookUrl ? `- Facebook: ${settings.facebookUrl}` : '',
    settings?.youtubeUrl ? `- YouTube: ${settings.youtubeUrl}` : '',
    settings?.whatsappNumber ? `- WhatsApp: https://wa.me/${settings.whatsappNumber}` : '',
  ].filter(Boolean).join('\n');

  const content = `# ${siteName}

> ${settings?.metaDescTr || "Akyaka'nın kalbinde doğayla iç içe lüks bir beach, restoran ve otel deneyimi."}

${siteName} is a luxury beach, restaurant and hotel located in Akyaka, Muğla, Turkey. 
Situated at the heart of the Akyaka coast, it offers a unique blend of natural beauty, 
fine dining, and premium hospitality.

## Important Links

- Homepage (TR): ${siteUrl}/tr
- Homepage (EN): ${siteUrl}/en
- Sitemap: ${siteUrl}/sitemap.xml

## Services

${serviceList}

## Contact Information

${contactLines || '- İletişim bilgileri için siteyi ziyaret edin.'}

## Social Media

${socialLines || '- Sosyal medya bilgileri için siteyi ziyaret edin.'}

## Location

- Şehir: Akyaka, Muğla, Türkiye
- Bölge: Ege Bölgesi, Türkiye
- Koordinatlar: 37.0437° N, 28.3249° E

## Language Support

This website is available in Turkish (TR) and English (EN).

## About

${siteName} offers an exclusive beachside experience in one of Turkey's most 
pristine coastal towns — Akyaka. Known for its crystal-clear waters, 
fresh Ula wind, and untouched nature, Akyaka is a hidden gem on the 
Aegean coast. ${siteName} brings luxury and comfort to this natural paradise.

---
Generated: ${new Date().toISOString()}
`;

  return new NextResponse(content, {
    status: 200,
    headers: {
      'Content-Type': 'text/plain; charset=utf-8',
      'Cache-Control': 'public, max-age=3600, stale-while-revalidate=86400',
    },
  });
}
