import { sql } from '../sql.js';

const SCRYFALL_DELAY_MS = 100;
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function fetchScryfallWithRetry(url, maxRetries = 3) {
  for (let attempt = 1; attempt <= maxRetries; attempt += 1) {
    const response = await fetch(url, {
      headers: { 'User-Agent': 'DeckHearth/1.0', Accept: 'application/json' },
    });

    if (response.ok) return response;

    if (response.status === 429 || response.status === 503) {
      const retryAfter = parseInt(response.headers.get('retry-after') || '2', 10);
      const waitMs = Math.max(retryAfter * 1000, 1000 * attempt);
      console.warn(`[fetchScryfallWithRetry] ${response.status} on attempt ${attempt}, waiting ${waitMs}ms`);
      await delay(waitMs);
      continue;
    }

    if (response.status === 404) {
      return response;
    }

    throw new Error(`Scryfall API error: ${response.status} for ${url}`);
  }
  throw new Error(`Scryfall API: exhausted ${maxRetries} retries for ${url}`);
}

async function fetchAllScryfallPages(initialUrl) {
  const allCards = [];
  let url = initialUrl;

  while (url) {
    const response = await fetchScryfallWithRetry(url);
    if (!response.ok) break;

    const data = await response.json();
    allCards.push(...(data.data || []));

    if (data.has_more && data.next_page) {
      url = data.next_page;
      await delay(SCRYFALL_DELAY_MS);
    } else {
      url = null;
    }
  }

  return allCards;
}

/**
 * Import all cards for a Scryfall set code. Skips rows already present by scryfall_id.
 */
export async function importMtgSet(setCode) {
  const initialUrl = `https://api.scryfall.com/cards/search?q=set:${encodeURIComponent(setCode)}`;
  const firstResponse = await fetchScryfallWithRetry(initialUrl);

  if (firstResponse.status === 404) {
    return { setCode, imported: 0, skipped: 0, total: 0 };
  }

  if (!firstResponse.ok) {
    throw new Error(`Scryfall API error: ${firstResponse.status} for set ${setCode}`);
  }

  const firstPage = await firstResponse.json();
  let cards = firstPage.data || [];

  if (firstPage.has_more && firstPage.next_page) {
    await delay(SCRYFALL_DELAY_MS);
    const remaining = await fetchAllScryfallPages(firstPage.next_page);
    cards = cards.concat(remaining);
  }

  let imported = 0;
  let skipped = 0;

  for (const card of cards) {
    try {
      const existingCard = await sql`
        SELECT id FROM cards WHERE scryfall_id = ${card.id}
      `;

      if (existingCard.rows.length > 0) {
        skipped += 1;
        continue;
      }

      let currentPrice = null;
      let marketPrice = null;

      if (card.prices) {
        currentPrice = card.prices.usd ? parseFloat(card.prices.usd) : null;
        marketPrice = card.prices.usd_foil ? parseFloat(card.prices.usd_foil) : null;
      }

      await sql`
        INSERT INTO cards (
          name, set_name, set_code, card_number, rarity, game,
          mana_cost, cmc, card_type, colors, oracle_text,
          power, toughness, image_url, stock_image_url,
          current_price, market_price, scryfall_id, verified
        ) VALUES (
          ${card.name}, ${card.set_name}, ${card.set}, ${card.collector_number},
          ${card.rarity}, 'MTG', ${card.mana_cost || null}, ${card.cmc || null},
          ${card.type_line}, ${JSON.stringify(card.colors || [])},
          ${card.oracle_text || null}, ${card.power || null}, ${card.toughness || null},
          ${card.image_uris?.normal || null}, ${card.image_uris?.art_crop || null},
          ${currentPrice}, ${marketPrice}, ${card.id}, true
        )
      `;

      imported += 1;
    } catch (error) {
      console.error(`[importMtgSet] Error importing card ${card.name}:`, error);
      skipped += 1;
    }
  }

  return { setCode, imported, skipped, total: cards.length };
}
