export type GitHubAccessTokenResponse = {
  access_token: string;
  scope: string;
  token_type: string;
};

export type GitHubUserResponse = {
  login: string;
  id: number;
  avatar_url: string;
  email: string;
};

export async function getGitHubUser(token: string) {
  const req = await fetch("https://api.github.com/user", {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
  const res: GitHubUserResponse = await req.json();
  return res;
}

export async function checkInOrg(username: string) {
  const req = await fetch(
    `https://api.github.com/orgs/${process.env.GITHUB_ORG}/members`,
    {
      headers: {
        Authorization: `Bearer ${process.env.GITHUB_TOKEN}`
      }
    }
  );

  const res: GitHubUserResponse[] = await req.json();
  return res.some((user) => user.login === username);
}

export async function getGitHubAvatar(token: string) {
  const user = await getGitHubUser(token);
  return user.avatar_url;
}
