// Shared test fixture: the analytics suites insert keywords / analytics_jobs
// for a fixed SITE id. Both tables carry a FK to `sites` (and `sites` to
// `organization` + `user`), so the parent rows must exist or the inserts fail
// — this used to "work" only because partitioned `keywords` had lost its FK to
// `sites` (restored in migration 0043). Seed the chain idempotently.
import { organization, user } from "@/lib/db/auth-schema";
import { sites } from "@/lib/db/domain-schema";
import { db } from "../../src/db";

export const TEST_SITE = "ac5b0f4a-c760-459d-80e2-43786951381f";
export const TEST_ORG = "analytics-test-org";
export const TEST_USER = "analytics-test-owner";

/** Ensure the org → user → site chain exists so FK-bound inserts succeed. */
export async function ensureTestSite(): Promise<void> {
  await db
    .insert(user)
    .values({
      id: TEST_USER,
      name: "Analytics Test Owner",
      email: "analytics-test@seo-insight.local",
      emailVerified: true,
      createdAt: new Date(),
      updatedAt: new Date(),
    })
    .onConflictDoNothing();
  await db
    .insert(organization)
    .values({
      id: TEST_ORG,
      name: "Analytics Test Org",
      slug: "analytics-test",
      createdAt: new Date(),
    })
    .onConflictDoNothing();
  await db
    .insert(sites)
    .values({
      id: TEST_SITE,
      orgId: TEST_ORG,
      ownerUserId: TEST_USER,
      name: "Analytics Test Site",
      url: "https://analytics-test.local",
      primaryLanguage: "en",
      languages: [],
      targetCountries: ["US"],
    })
    .onConflictDoNothing();
}
