import { ok, type ReadonlyDB } from "@tailor-platform/erp-kit/core";
import type { DB } from "../generated/kysely-tailordb";

export type GetSlackWorkspaceIntegrationInput = Record<string, never>;

export async function run(db: ReadonlyDB<DB>, input: GetSlackWorkspaceIntegrationInput) {
  void input;
  const row = await db
    .selectFrom("SlackWorkspaceIntegration")
    .select([
      "id",
      "teamId",
      "teamName",
      "botUserId",
      "status",
      "installedAt",
      "lastValidatedAt",
      "revokedAt",
      "createdAt",
      "updatedAt",
    ])
    .orderBy("createdAt")
    .limit(1)
    .executeTakeFirst();

  return ok({ slackWorkspaceIntegration: row ?? null });
}
