import axios from 'axios';
import dotenv from 'dotenv';
import { blogPostList } from '../lib/blog-data';

dotenv.config();

const INDEXNOW_ENDPOINT = 'https://api.indexnow.org/indexnow';
const HOST = 'www.qrmaster.net';
// You need to generate a key from https://www.bing.com/indexnow and place it in your public folder
// For now, we'll assume a key exists or is provided via env
const KEY = process.env.INDEXNOW_KEY || 'your-indexnow-key';
const KEY_LOCATION = `https://${HOST}/${KEY}.txt`;

export async function submitToIndexNow(urls: string[]) {
    try {
        const payload = {
            host: HOST,
            key: KEY,
            keyLocation: KEY_LOCATION,
            urlList: urls,
        };

        console.log(`Submitting ${urls.length} URLs to IndexNow...`);
        const response = await axios.post(INDEXNOW_ENDPOINT, payload, {
            headers: {
                'Content-Type': 'application/json',
            },
        });

        if (response.status === 200 || response.status === 202) {
            console.log('✅ Successfully submitted URLs to IndexNow.');
        } else {
            console.error(`⚠️ IndexNow submission returned status: ${response.status}`);
        }
    } catch (error) {
        if (axios.isAxiosError(error)) {
            console.error('❌ Error submitting to IndexNow:', error.message);
            console.error('Response data:', error.response?.data);
        } else {
            console.error('❌ Unknown error submitting to IndexNow:', error);
        }
    }
}

// Function to gather all indexable URLs
export function getAllIndexableUrls(): string[] {
    const baseUrl = `https://${HOST}`;

    // Free tools
    const freeTools = [
        'url-qr-code', 'vcard-qr-code', 'text-qr-code', 'email-qr-code', 'sms-qr-code',
        'wifi-qr-code', 'crypto-qr-code', 'event-qr-code', 'facebook-qr-code',
        'instagram-qr-code', 'twitter-qr-code', 'youtube-qr-code', 'whatsapp-qr-code',
        'tiktok-qr-code', 'geolocation-qr-code', 'phone-qr-code', 'paypal-qr-code',
        'zoom-qr-code', 'teams-qr-code',
    ].map(slug => `${baseUrl}/tools/${slug}`);

    // Blog posts
    const blogPages = blogPostList.map(post => `${baseUrl}/blog/${post.slug}`);

    // Main pages
    const mainPages = [
        baseUrl,
        `${baseUrl}/qr-code-erstellen`,
        `${baseUrl}/qr-code-tracking`,
        `${baseUrl}/pricing`,
        `${baseUrl}/bulk-qr-code-generator`,
        `${baseUrl}/dynamic-qr-code-generator`,
        `${baseUrl}/reprint-calculator`,
        `${baseUrl}/faq`,
        `${baseUrl}/blog`,
        `${baseUrl}/signup`,
        `${baseUrl}/login`,
    ];

    return [...mainPages, ...freeTools, ...blogPages];
}

// If run directly
if (require.main === module) {
    const urls = getAllIndexableUrls();
    submitToIndexNow(urls);
}
