import { eq } from "drizzle-orm";
import { describe, expect, it } from "vitest";

import { db } from "@/lib/db";
import { appConfig, category } from "@/lib/db/schema";

describe("integration harness smoke test", () => {
  it("connects to the migrated test database and performs CRUD", async () => {
    const [created] = await db
      .insert(category)
      .values({
        slug: "smoke",
        nameAr: "a",
        nameEn: "b",
        nameDe: "c",
        descriptionAr: "a",
        descriptionEn: "b",
        descriptionDe: "c",
      })
      .returning();

    expect(created.id).toBeTruthy();
    expect(created.isActive).toBe(true);

    const found = await db.query.category.findFirst({ where: eq(category.slug, "smoke") });
    expect(found?.nameEn).toBe("b");
  });

  it("resets the database between tests", async () => {
    const count = await db.$count(category);
    expect(count).toBe(0);
  });

  it("supports enums and appconfig upsert", async () => {
    await db
      .insert(appConfig)
      .values({ key: "k", value: "v1" })
      .onConflictDoUpdate({ target: appConfig.key, set: { value: "v2" } });
    const row = await db.query.appConfig.findFirst({ where: eq(appConfig.key, "k") });
    expect(row?.value).toBe("v1");
  });
});
