// Phase 12 Plan 03 — Task 1 verification script for offer-queries.ts additions.
//
// This project has no test runner configured (no `scripts.test` in package.json),
// and `.env.local`'s DATABASE_URL points at PRODUCTION (per project memory) — so
// this script does NOT execute any DB calls. It typechecks against the real
// exports/types from src/lib/offer-queries.ts and documents the 5 expected
// behaviors from the plan's <behavior> block, for manual run against a dev DB
// later if desired.
//
// Run (manual, dev DB only): npx tsx scripts/verify-12-03-queries.ts

import {
  getOfferEditorData,
  getOfferListCards,
  getOfferFieldOptions,
  type OfferEditorData,
  type OfferListCard,
  type OfferFieldOptions,
  type OfferTierData,
} from "@/lib/offer-queries";

// ── Test 1: getOfferListCards() ──────────────────────────────────────────────
// Input: 2 offer_macros rows (1 with is_archived=true, 1 with is_archived=false).
// Expected: array of length 2, both present, ordered by sort_order, each row
// shaped { id, internal_name, description, category, is_archived }. Archived
// rows ARE returned — filtering is client-side (UI-SPEC "Mostra offerte archiviate").
async function test1_getOfferListCards() {
  const cards: OfferListCard[] = await getOfferListCards();
  // Expected assertions (manual):
  //   cards.length === 2
  //   cards.some(c => c.is_archived === true)
  //   cards.some(c => c.is_archived === false)
  return cards;
}

// ── Test 2: getOfferEditorData(macroId) — macro with 0 tiers ─────────────────
// Input: macro with category = "Signature Offer", 0 offer_micros rows, 3
// services with category="Signature Offer" + 2 with a different category.
// Expected: { macro: {...}, tiers: [], availableServices: [...3 matching...],
// tipoTags: [], obiettivoTags: [] }
async function test2_emptyTiers(macroId: string) {
  const data: OfferEditorData | null = await getOfferEditorData(macroId);
  // Expected assertions (manual):
  //   data !== null
  //   data.tiers.length === 0
  //   data.availableServices.length === 3  (only services.category === macro.category)
  //   data.tipoTags.length === 0 && data.obiettivoTags.length === 0
  return data;
}

// ── Test 3: getOfferEditorData(macroId) — 3 tiers, tier A has 2 services ─────
// Input: macro with 3 offer_micros rows (tier_letter A/B/C) and 2
// offer_tier_services rows assigned to tier A.
// Expected: tiers sorted A->B->C; each tier has
// { id, tier_letter, public_price, assignedServiceIds, servicesTotal };
// tier A's servicesTotal === sum(unit_price) of its 2 assigned services;
// tiers B/C have assignedServiceIds === [] and servicesTotal === "0".
async function test3_tiersWithServices(macroId: string) {
  const data: OfferEditorData | null = await getOfferEditorData(macroId);
  // Expected assertions (manual):
  //   data.tiers.map(t => t.tier_letter) === ["A", "B", "C"]
  //   data.tiers[0].assignedServiceIds.length === 2
  //   Number(data.tiers[0].servicesTotal) === sum of the 2 services' unit_price
  //   data.tiers[1].assignedServiceIds === [] && data.tiers[1].servicesTotal === "0"
  //   data.tiers[2].assignedServiceIds === [] && data.tiers[2].servicesTotal === "0"
  const tierA: OfferTierData | undefined = data?.tiers[0];
  return { data, tierA };
}

// ── Test 4: getOfferEditorData(macroId) — tipoTags/obiettivoTags ─────────────
// Input: 2 "tipo" tags + 1 "obiettivo" tag exist for the macro (entity_type =
// "offer_macros.tipo" / "offer_macros.obiettivo", entity_id = macroId).
// Expected: tipoTags.length === 2, obiettivoTags.length === 1.
async function test4_tags(macroId: string) {
  const data: OfferEditorData | null = await getOfferEditorData(macroId);
  // Expected assertions (manual):
  //   data?.tipoTags.length === 2
  //   data?.obiettivoTags.length === 1
  return data;
}

// ── Test 5: getOfferFieldOptions() ───────────────────────────────────────────
// Input: 2 macros with category "Entry Offer"/"Signature Offer", 1 "tipo" tag
// "Audit", 1 "obiettivo" tag "Lead Generation".
// Expected: categoria contains both values; tipo === ["Audit"];
// obiettivo === ["Lead Generation"].
async function test5_fieldOptions() {
  const options: OfferFieldOptions = await getOfferFieldOptions();
  // Expected assertions (manual):
  //   options.categoria includes "Entry Offer" and "Signature Offer"
  //   options.tipo includes "Audit"
  //   options.obiettivo includes "Lead Generation"
  return options;
}

// Not executed automatically (production DB) — typecheck-only verification.
// To run manually against a dev DB: uncomment the call below.
// void (async () => {
//   await test1_getOfferListCards();
//   await test2_emptyTiers("macro-id");
//   await test3_tiersWithServices("macro-id");
//   await test4_tags("macro-id");
//   await test5_fieldOptions();
// })();

export {
  test1_getOfferListCards,
  test2_emptyTiers,
  test3_tiersWithServices,
  test4_tags,
  test5_fieldOptions,
};
