#!/usr/bin/env python3
"""Github repo <user/repo> lookup.

---
source: https://github.com/terror/dotfiles
"""

import json  # noqa: F401
import sys
from dataclasses import dataclass
from textwrap import dedent  # noqa: F401

import requests


def e(*args, **kwargs):
    print(*args, **kwargs, file=sys.stderr)


def die(*args, **kwargs):
    e(*args, **kwargs)
    sys.exit(-1)


@dataclass
class Repo:
    name: str
    stars: int
    watchers: int
    lang: str
    license: str
    clone_url: str

    def __str__(self):
        return (
            f"{self.name} [{self.lang}]\n"
            f"⭐{self.stars}\n"
            f"🔭 {self.watchers}\n"
            f"📜 {self.license.upper()}"
        )

    def create(self):
        return Repo(
            name=self["full_name"],
            stars=self["stargazers_count"],
            watchers=self["watchers"],
            lang=self["language"],
            license=self["license"]["key"],
            clone_url=self["clone_url"],
        )


def main(query):
    user, repo = query.split("/")
    response = requests.get(f"https://api.github.com/repos/{user}/{repo}", timeout=60)
    response.raise_for_status()
    print(Repo.create(response.json()))


if __name__ == "__main__":
    if len(sys.argv) > 2:
        die("Usage: repo <user/repo>")
    try:
        main(sys.argv[1])
    except (Exception, requests.exceptions.HTTPError) as error:  # noqa: BLE001
        die(f"Fatal error: {error}.")
