import { afterEach, describe, expect, it } from "vitest";
import { TestSqliteDatabase } from "./test-support.js";
import { SsoProviderRepository } from "../../../database/repositories/sso-provider-repository.js";

describe("SsoProviderRepository", () => {
  let adapter: TestSqliteDatabase | null = null;

  afterEach(async () => {
    if (adapter) {
      await adapter.close();
      adapter = null;
    }
  });

  async function createRepository(
    onWrite?: () => void | Promise<void>,
  ): Promise<SsoProviderRepository> {
    adapter = new TestSqliteDatabase();
    const context = await adapter.connect();

    return new SsoProviderRepository(context, onWrite);
  }

  it("creates, lists, finds, updates, and deletes providers", async () => {
    const repo = await createRepository();

    const disabled = await repo.create({
      name: "Disabled",
      type: "oidc",
      enabled: false,
      displayOrder: 1,
      config: "{}",
    });
    const enabled = await repo.create({
      name: "GitHub",
      type: "github",
      enabled: true,
      displayOrder: 0,
      config: '{"client_id":"id"}',
    });

    expect((await repo.listEnabledPublic()).map((row) => row.id)).toEqual([
      enabled.id,
    ]);
    expect((await repo.listAll()).map((row) => row.id)).toEqual([
      enabled.id,
      disabled.id,
    ]);
    expect((await repo.findById(enabled.id))?.name).toBe("GitHub");
    expect((await repo.findFirstEnabledOidcLike())?.id).toBe(enabled.id);

    const updated = await repo.update(enabled.id, {
      name: "GitHub SSO",
      config: '{"client_id":"updated"}',
      updatedAt: "2026-06-27T00:00:00.000Z",
    });
    expect(updated?.name).toBe("GitHub SSO");
    expect(updated?.config).toContain("updated");

    expect(await repo.delete(enabled.id)).toBe(true);
    expect(await repo.findById(enabled.id)).toBeNull();
    expect(await repo.delete(enabled.id)).toBe(false);
  });

  it("counts users associated with a provider", async () => {
    const repo = await createRepository();
    const provider = await repo.create({
      name: "LDAP",
      type: "ldap",
      enabled: true,
      displayOrder: 0,
      config: "{}",
    });

    await adapter!.exec(`
      INSERT INTO users (id, username, password_hash, sso_provider_id)
      VALUES ('user-1', 'u1', 'hash', ${provider.id}),
             ('user-2', 'u2', 'hash', ${provider.id}),
             ('user-3', 'u3', 'hash', NULL);
    `);

    expect(await repo.countUsersByProviderId(provider.id)).toBe(2);
  });

  it("runs the write hook after provider writes", async () => {
    let writeCount = 0;
    const repo = await createRepository(() => {
      writeCount += 1;
    });

    const provider = await repo.create({
      name: "OIDC",
      type: "oidc",
      enabled: true,
      displayOrder: 0,
      config: "{}",
    });
    await repo.update(provider.id, { enabled: false });
    await repo.delete(provider.id);
    await repo.delete(provider.id);

    expect(writeCount).toBe(3);
  });
});
