import fs from 'fs';
import { CALCULATORS } from '../src/constants';

const BASE_URL = 'https://dealworthtools.com';
const TODAY = new Date().toISOString().split('T')[0];

interface SitemapUrl {
  loc: string;
  lastmod: string;
  changefreq: 'daily' | 'weekly' | 'monthly' | 'yearly';
  priority: number;
}

const urls: SitemapUrl[] = [
  // Core Pages
  {
    loc: '/',
    lastmod: TODAY,
    changefreq: 'daily',
    priority: 1.0
  },
  {
    loc: '/scenarios',
    lastmod: TODAY,
    changefreq: 'weekly',
    priority: 0.7
  },
  {
    loc: '/contact',
    lastmod: TODAY,
    changefreq: 'monthly',
    priority: 0.5
  },

  // Guides
  {
    loc: '/guides/ugc-rate-card-2026',
    lastmod: TODAY,
    changefreq: 'monthly',
    priority: 0.8
  },
  {
    loc: '/guides/usage-rights-pricing-explained',
    lastmod: TODAY,
    changefreq: 'monthly',
    priority: 0.8
  },
  {
    loc: '/guides/freelancer-package-pricing-playbook',
    lastmod: TODAY,
    changefreq: 'monthly',
    priority: 0.8
  },

  // Hubs
  {
    loc: '/hubs/ugc-rates-by-region',
    lastmod: TODAY,
    changefreq: 'monthly',
    priority: 0.8
  },
  {
    loc: '/hubs/ugc-rates-by-niche',
    lastmod: TODAY,
    changefreq: 'monthly',
    priority: 0.8
  },

  // Reports
  {
    loc: '/reports/dealworthtools-baseline-pricing-model-2026',
    lastmod: TODAY,
    changefreq: 'monthly',
    priority: 0.8
  },

  // Legal
  {
    loc: '/terms',
    lastmod: '2026-01-01',
    changefreq: 'yearly',
    priority: 0.3
  },
  {
    loc: '/privacy',
    lastmod: '2026-01-01',
    changefreq: 'yearly',
    priority: 0.3
  },
  {
    loc: '/cookies',
    lastmod: '2026-01-01',
    changefreq: 'yearly',
    priority: 0.3
  },

  // Calculators (동적 생성)
  ...CALCULATORS.map(calc => ({
    loc: `/calculator/${calc.slug}`,
    lastmod: TODAY,
    changefreq: 'weekly' as const,
    priority: 0.9
  }))
];

// XML 생성
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
        xmlns:xhtml="http://www.w3.org/1999/xhtml"
        xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
${urls.map(url => `  <url>
    <loc>${BASE_URL}${url.loc}</loc>
    <lastmod>${url.lastmod}</lastmod>
    <changefreq>${url.changefreq}</changefreq>
    <priority>${url.priority}</priority>
  </url>`).join('\n')}
</urlset>`;

// 파일 저장
fs.writeFileSync('public/sitemap.xml', sitemap);
console.log(`✅ Sitemap generated with ${urls.length} URLs`);

// 자동 핑 (배포 후)
async function pingSitemaps() {
  const sitemapUrl = encodeURIComponent(`${BASE_URL}/sitemap.xml`);
  
  try {
    // Google 핑
    await fetch(`https://www.google.com/ping?sitemap=${sitemapUrl}`);
    console.log('✅ Pinged Google');
    
    // Bing 핑
    await fetch(`https://www.bing.com/ping?sitemap=${sitemapUrl}`);
    console.log('✅ Pinged Bing');
  } catch (error) {
    console.error('❌ Ping failed:', error);
  }
}

// 프로덕션 빌드 시 자동 핑
if (process.env.NODE_ENV === 'production') {
  pingSitemaps();
}
