// src/lib/auth/account-delete-guard.test.ts — deleteUser.beforeDelete guard
// (P0 S1) against REAL data in Postgres. Gated on DB_TESTS=1, test DB only:
// DB_TESTS=1 DATABASE_URL=postgresql://humperdoo@localhost:5432/seo_insight_test \
//   bun --conditions=react-server test <file>
import { beforeEach, describe, expect, mock, test } from "bun:test";

mock.module("server-only", () => ({}));

const RUN_DB = process.env.DB_TESTS === "1";

const ORG = "test-adg-org";
const OWNER = "test-adg-owner"; // owns a site + a ticket
const BYSTANDER = "test-adg-bystander"; // owns nothing
const NOW = new Date();

describe.skipIf(!RUN_DB)("account-delete guard (postgres)", () => {
  let db: typeof import("@/lib/db/client").db;
  let auth: typeof import("@/lib/db/auth-schema");
  let schema: typeof import("@/lib/db/domain-schema");
  let guard: typeof import("./account-delete-guard");

  async function seedUser(id: string, email: string) {
    await db.insert(auth.user).values({
      id,
      name: id,
      email,
      emailVerified: true,
      createdAt: NOW,
      updatedAt: NOW,
    });
  }

  beforeEach(async () => {
    ({ db } = await import("@/lib/db/client"));
    auth = await import("@/lib/db/auth-schema");
    schema = await import("@/lib/db/domain-schema");
    guard = await import("./account-delete-guard");
    const { assertTestDb } = await import("@/lib/db/assert-test-db");
    const { eq, inArray } = await import("drizzle-orm");
    await assertTestDb();

    // Org cascades to sites (orgId, cascade); users are deleted explicitly.
    await db.delete(auth.organization).where(eq(auth.organization.id, ORG));
    await db.delete(auth.user).where(inArray(auth.user.id, [OWNER, BYSTANDER]));

    await seedUser(OWNER, "adg-owner@test.dev");
    await seedUser(BYSTANDER, "adg-bystander@test.dev");
    await db.insert(auth.organization).values({
      id: ORG,
      name: "Account Delete Guard Org",
      slug: "test-account-delete-guard",
      createdAt: NOW,
    });
  });

  test("user who owns a site + a ticket: counted, deletion still allowed (no throw)", async () => {
    await db.insert(schema.sites).values({
      id: "test-adg-site",
      orgId: ORG,
      ownerUserId: OWNER,
      name: "Owned Site",
      primaryLanguage: "en",
    });
    await db.insert(schema.supportTickets).values({
      id: "test-adg-ticket",
      title: "Help",
      status: "open",
      priority: "medium",
      userId: OWNER,
      orgId: ORG,
    });

    const ownership = await guard.getAccountDeletionOwnership(OWNER);
    expect(ownership).toEqual({ siteCount: 1, ticketCount: 1 });

    // The hook never blocks — it must resolve without throwing.
    await expect(
      guard.logAccountDeletionOwnership(OWNER),
    ).resolves.toBeUndefined();
  });

  test("user who owns nothing: zero counts, deletion allowed", async () => {
    const ownership = await guard.getAccountDeletionOwnership(BYSTANDER);
    expect(ownership).toEqual({ siteCount: 0, ticketCount: 0 });

    await expect(
      guard.logAccountDeletionOwnership(BYSTANDER),
    ).resolves.toBeUndefined();
  });
});
