import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";

const css = await readFile(new URL("../app/globals.css", import.meta.url), "utf8");
const renderer = await readFile(new URL("../app/[...slug]/page.tsx", import.meta.url), "utf8");
const leadsApi = await readFile(new URL("../app/api/leads/route.ts", import.meta.url), "utf8");
const schema = await readFile(new URL("../db/schema.ts", import.meta.url), "utf8");
const checkoutApi = await readFile(new URL("../app/api/checkout/route.ts", import.meta.url), "utf8");
const stripeWebhook = await readFile(new URL("../app/api/stripe/webhook/route.ts", import.meta.url), "utf8");
const checkoutStatus = await readFile(new URL("../app/api/checkout/status/route.ts", import.meta.url), "utf8");
const commerceCatalog = await readFile(new URL("../lib/commerce.ts", import.meta.url), "utf8");
const interactiveBlocks = await readFile(new URL("../app/components/InteractiveBlocks.tsx", import.meta.url), "utf8");
const rootLeadForm = await readFile(new URL("../app/components/LeadForm.tsx", import.meta.url), "utf8");

test("includes responsive and reduced-motion visual rules", () => {
  assert.match(css, /@media\(max-width:720px\)/);
  assert.match(css, /@media\(prefers-reduced-motion:reduce\)/);
  assert.match(css, /\.inner-hero/);
  assert.match(css, /\.diagnosis-tool/);
  assert.match(css, /\.commerce-gate/);
});

test("renders forms, gates, diagnosis and legal states", () => {
  assert.match(renderer, /DynamicLeadForm/);
  assert.match(renderer, /DiagnosisTool/);
  assert.match(renderer, /CommerceGate/);
  assert.match(renderer, /CookiePreferences/);
  assert.match(renderer, /ProtectedHub/);
  assert.match(renderer, /JsonLd/);
});

test("stores leads with separate channel consents and attribution", () => {
  assert.match(leadsApi, /consentWhatsapp/);
  assert.match(leadsApi, /consentSms/);
  assert.match(leadsApi, /creativeId/);
  assert.match(leadsApi, /detailsJson/);
  assert.match(schema, /consent_version/);
  assert.match(schema, /utm_campaign/);
  assert.match(leadsApi, /experimentId/);
  assert.match(leadsApi, /experimentVariant/);
  assert.match(leadsApi, /assignmentMethod/);
  assert.match(schema, /experiment_id/);
  assert.match(interactiveBlocks, /leadExperimentAttribution\(assignment\)/);
  assert.match(rootLeadForm, /leadExperimentAttribution\(assignment\)/);
});

test("does not emit purchase events from the public web renderer", () => {
  assert.doesNotMatch(renderer, /event:\s*["']purchase["']/i);
  assert.doesNotMatch(renderer, /Purchase\s*\(/);
});

test("keeps checkout closed and records payment only through a signed webhook", () => {
  assert.match(checkoutApi, /CHECKOUT_OPEN/);
  assert.match(checkoutApi, /catalogEntryForTier/);
  assert.match(commerceCatalog, /STRIPE_PRICE_ESSENCIAL/);
  assert.match(checkoutApi, /automatic_payment_methods/);
  assert.match(stripeWebhook, /stripe-signature/);
  assert.match(stripeWebhook, /checkout\.session\.completed/);
  assert.match(stripeWebhook, /onConflictDoNothing/);
});

test("reconciles the Stripe catalogue before accepting or confirming payment", () => {
  assert.match(commerceCatalog, /STRIPE_AMOUNT_ESSENCIAL_CENTS/);
  assert.match(commerceCatalog, /STRIPE_CURRENCY/);
  assert.match(stripeWebhook, /stripe_price_not_in_catalog/);
  assert.match(stripeWebhook, /stripe_amount_or_currency_mismatch/);
  assert.match(stripeWebhook, /stripe_tier_metadata_mismatch/);
  assert.match(checkoutStatus, /paymentStatus !== "paid"/);
  assert.match(checkoutStatus, /stripe_confirmation_reconciliation_failed/);
});

test("starts Consent Mode denied and treats necessary processing as acknowledgement", () => {
  assert.match(interactiveBlocks, /publishConsent\("default", false, false\)/);
  assert.match(interactiveBlocks, /ad_user_data/);
  assert.match(interactiveBlocks, /publishConsent\("update", analytics, advertising\)/);
  assert.match(rootLeadForm, /Li e compreendo/);
  assert.doesNotMatch(rootLeadForm, /Quero receber a informação operacional necessária/);
  assert.match(interactiveBlocks, /if \(!analyticsConsentGranted\(\)\) return false/);
  assert.match(interactiveBlocks, /comando:consent-updated/);
});

test("keeps gated draft prices out of the Client Component payload", () => {
  assert.match(renderer, /toPublicTicketTiers\(comando\.ticket_model\.tiers\)/);
  assert.doesNotMatch(renderer, /tiers=\{comando\.ticket_model\.tiers\}/);
});
