import { Effect } from "effect";

import { parseServerEnv } from "../env";
import { Gitea } from "../git/gitea-service";
import type { GiteaConfig } from "../git/gitea-service";

const env = parseServerEnv();

const config: GiteaConfig = {
  baseUrl: env.GITEA_URL,
  repoPath: env.GIT_REPO,
  token: env.GITEA_TOKEN,
};

const giteaLayer = Gitea.layer(config);

/** Run any Effect that depends on the Gitea service. */
export const runWithGitea = <A, E>(
  effect: Effect.Effect<A, E, Gitea>
): Promise<A> => Effect.runPromise(effect.pipe(Effect.provide(giteaLayer)));

export { env };
