export const env = {
  OAUTH_PROVIDER: process.env.OAUTH_PROVIDER,
  OAUTH_CLIENT_ID: process.env.OAUTH_CLIENT_ID,
  OAUTH_CLIENT_SECRET: process.env.OAUTH_CLIENT_SECRET,
  OAUTH_AUTHORIZE_URL: process.env.OAUTH_AUTHORIZE_URL,
  OAUTH_TOKEN_URL: process.env.OAUTH_TOKEN_URL,
  OAUTH_USERINFO_URL: process.env.OAUTH_USERINFO_URL,
  OAUTH_ALLOWED_USERS: process.env.OAUTH_ALLOWED_USERS,
  OAUTH_ALLOWED_ORG: process.env.OAUTH_ALLOWED_ORG,
  PUBLIC_BASE_URL: process.env.PUBLIC_BASE_URL,
  GITEA_BASE: process.env.GITEA_BASE,
  GITEA_OWNER: process.env.GITEA_OWNER,
  GITEA_REPO: process.env.GITEA_REPO,
  GITEA_TOKEN: process.env.GITEA_TOKEN,
  GIT_BRANCH: process.env.GIT_BRANCH || 'main',
  SESSION_SECRET: process.env.SESSION_SECRET,
  CSRF_SECRET: process.env.CSRF_SECRET,
};

export function getBaseUrlFromRequest(req: Request): string {
  try {
    if (env.PUBLIC_BASE_URL) return new URL(env.PUBLIC_BASE_URL).toString().replace(/\/$/, '');
  } catch {}
  const url = new URL(req.url);
  url.pathname = '';
  url.search = '';
  url.hash = '';
  return url.toString().replace(/\/$/, '');
}
