# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/01_GroupEng_assign.ipynb.

# %% auto 0
__all__ = ['AssignGroup']

# %% ../nbs/api/01_GroupEng_assign.ipynb 3
import GroupEng
import canvasapi
import github
from . import GitHubGroup, CanvasGroup

# %% ../nbs/api/01_GroupEng_assign.ipynb 4
class AssignGroup:
    def __init__(self,
                 ghg: GitHubGroup, # authenticated GitHub object
                 cg: CanvasGroup, # authenticated canvas object
                 groupeng_config="", # Directory for the GroupEng config yml file
                ):
        "Initializer for Assign Group"
        self.status = None
        self.out_dir = None
        self.prefix = None
        self.cg = cg
        self.ghg = ghg
        # Initialize if appropriate parameters are defined
        if groupeng_config != "":
            self.assign_groups(groupeng_config)

    def assign_groups(self,
                      groupeng_config:str, # Directory for the GroupEng config yml file
                      assign_canvas_group=False, # directly assign canvas groups
                      create_gh_repo=False, # directly create GitHub repos
                      username_quiz_id=-1, # username quiz id from canvas course
                      in_group_category="", # specify which group category the group belongs to
                      suffix="", # suffix to the group name
                     ) -> (bool, str): # Status and output directory of the compiled file.
        status, out_dir = GroupEng.run(groupeng_config)
        self.status, self.out_dir = status, out_dir
        file = os.path.split(groupeng_config)[1]
        self.prefix = os.path.splitext(file)[0]
        if assign_canvas_group:
            if self.cg.group_category is None and in_group_category == "":
                raise ValueError("Have to specify in_group_category to create canvas group")
            self.create_canvas_group(in_group_category, suffix)
        if create_gh_repo:
            if username_quiz_id == -1:
                raise ValueError("Have to specify the canvas username quiz id")
            self.create_github_group(username_quiz_id)
        return status, out_dir

    def create_canvas_group(self,
                            in_group_category="", # specify which group category the group belongs to
                            suffix="", # suffix to the group name
                            ):
        "Create canvas groups based on the generated group configuration"
        if self.out_dir is None:
            raise ValueError("The group configuration has not been set. Please assign group via assign_groups")
        if self.cg.group_category is None:
            raise ValueError("The group category has not been set.")
        if in_group_category == "":
            in_group_category = self.cg.group_category.name
        # load the generated configuration file
        groups_generated_fp = os.path.join(self.out_dir, f"{self.prefix}_groups.csv")
        with open(groups_generated_fp, "r") as f:
            groups = f.read().splitlines()
        # create canvas groups for each.
        for group in groups:
            group = group.replace(" ", "").split(",")
            group_name, group_members = group[0], group[1:]
            self.cg.assign_canvas_group(
                group_name=f"{group_name}{suffix}",
                group_members=group_members,
                in_group_category=in_group_category
            )

    def create_github_group(self,
                            username_quiz_id:int # username quiz id from canvas course
                            ):
        github_usernames = self.cg.fetch_username_from_quiz(username_quiz_id)
        self.cg.set_group_category(cg.group_category.name)
        groups = self.cg.group_to_emails
        repos = []
        for group_name, members in groups.items():
            group_git_usernames = []
            for email in members:
                try:
                    # try to get the git username for each student.
                    # not all students completed their quiz.
                    group_git_usernames.append(github_usernames[email])
                except KeyError:
                    print(f"{email}'s GitHub Username not found")
            repo = self.ghg.create_group_repo(
                repo_name=group_name,
                collaborators=group_git_usernames,
                permission="write",
                repo_template="COGS118A/group_template",
                rename_files={
                    "Checkpoint_groupXXX.ipynb": f"Checkpoint_{group_name}.ipynb",
                    "FinalProject_groupXXX.ipynb": f"FinalProject_{group_name}.ipynb",
                    "Proposal_groupXXX.ipynb": f"Proposal_{group_name}.ipynb"
                },
                private=True,
                description=f"COGS118A Final Project {group_name} Repository",
                team_slug="Instructors_Sp23",
                team_permission="admin"
            )
            print("")
            repos.append(repo)
        return repos


