import git 
from tqdm import tqdm
import csv
import os
import shutil
import pandas as pd
import time

#destination DIR:
dest_dir="/data/users/mgaughan/kkex/012825_cam_revision_main/lagged_files/contributing/"
#temp DIR:
temp_dir="/data/users/mgaughan/tmp3/"

def temp_clone(vcs_link, temp_location):
    """
    ARGS
    vcs_link : url link to upstream repo vcs
    temp_location : filepath to where the repo should be cloned to 

    RETURNS
    repo : the GitRepository object of the cloned repo 
    repo_path : the filepath to the cloned repository
    """
    #print(temp_location)
    vcs_link = vcs_link.strip()
    os.makedirs(temp_location)
    repo_path = temp_location
    repo = git.Repo.clone_from(vcs_link, repo_path)
    print(f"Successfully Cloned {vcs_link}")
    return repo, repo_path


def delete_clone(temp_location):
    """
    ARGS
    temp_location : filepath to the cloned repository 

    RETURNS
    whether or not the deletion was a success
    """
    if os.path.exists(temp_location):
        shutil.rmtree(temp_location)
        print(f"{temp_location} has been deleted.")
        return 0
    else:
        print("No clone at location")
        return 1

# getting the specific readme or contributing file from a given commit 
# inputs: upstream vcs link, commit hash, yes/no is it a readme 
def get_file(vcs_link, commit_hash, is_readme, index):
    cwd = os.getcwd()
    os.environ['GIT_SSH_COMMAND'] = 'ssh -o StrictHostKeyChecking=no'
    os.environ['GIT_ASKPASS'] = 'false'
    os.environ['GIT_TERMINAL_PROMPT'] = '0'
    try:
        ssh_url = ""
        try: 
            if "github" in vcs_link:
                repo_id = vcs_link[len('https://github.com/'):]
                ssh_url = f'git@github.com:{repo_id}.git'
                if ssh_url.endswith('.git.git'):
                    ssh_url = ssh_url[:-4]
                temp_repo, temp_repo_path =  temp_clone(ssh_url, temp_dir)
            else:
                parts = vcs_link.split('/')
                domain = parts[2]
                repo_id = '/'.join(parts[3:])
                try:
                    temp_repo, temp_repo_path =  temp_clone(vcs_link, temp_dir)
                except Exception as e:
                    print(f'non-Github cloning error, assuming HTTPS issue: {e}')
                    delete_clone(temp_dir)
                    ssh_url = f'git@{domain}:{repo_id}.git'
                    if ssh_url.endswith('.git.git'):
                        ssh_url = ssh_url[:-4]
                    temp_repo, temp_repo_path =  temp_clone(ssh_url, temp_dir)
        except Exception as e:
            print(f'cloning error at {vcs_link}')
            print(f'inside cloning error: {e}')
            raise ValueError(e)
    
        os.chdir(temp_repo_path)
        reset_command = f"git reset --hard {commit_hash}"
        #print(reset_command)
        os.system(reset_command)

        doc_name = "CONTRIBUTING"
        if is_readme:
            doc_name = "README"
        
        und_repo_id = '_'.join(repo_id.split("/"))
        for file in os.listdir(temp_repo_path):
            if doc_name in file:
                doc_filename = file
                doc_path = os.path.join(temp_repo_path, file)
        dest_path = os.path.join(dest_dir, f"{index}_{und_repo_id}_{doc_filename}")
        shutil.copy(doc_path, dest_path)
    except Exception as e:
        print(f"outside cloning error: {e}")
        return False, ""
    finally:
        und_repo_id = ""
        delete_clone(temp_dir)
        os.chdir(cwd)
    return True, dest_path

def for_all_files(csv_path, is_readme):
    parsing_error = []
    doc = "CONTRIBUTING"
    if is_readme:
        doc = "README"
    try:
        with open(csv_path, 'r') as file:
            csv_reader = csv.DictReader(file) 
            lines = [line for line in file]
            new_manifest_list = []
            index = -1
            for row in tqdm(csv.reader(lines), total=len(lines)):
                index += 1
                if index < 1:
                    continue
                time.sleep(6)
                manifest_df = pd.DataFrame({
                    'commit_hash': [row[0]],
                    'upstream_vcs_link': [row[15]],
                    'repo_id': [row[13]],
                    'project_handle': [row[14]],
                    'lagged_hash': [row[12]],
                    'project_index': index
                })
                _check, new_filepath = get_file(manifest_df['upstream_vcs_link'][0], manifest_df['lagged_hash'][0], is_readme, index)
                manifest_df['new_filepath'] = new_filepath

                if _check == True:
                    new_manifest_list.append(manifest_df)
                else:
                    parsing_error.append([manifest_df['upstream_vcs_link'][0], manifest_df['lagged_hash'][0], str(index)])
    except KeyboardInterrupt:
        print("KeyBoardInterrrupt")
    finally:
        with open(f"s_{doc}-parsing-output.txt", "w") as txt_file:
                for error in parsing_error:
                    txt_file.write(', '.join(error) + "\n")
        manifest_df = pd.concat(new_manifest_list, ignore_index=True)
        manifest_df.to_csv(f"020125_{doc}_manifest.csv", index=False)

if __name__ == "__main__":
    for_all_files("/home/SOC.NORTHWESTERN.EDU/nws8519/git/24_deb_pkg_gov/12825_revision/misc_data_files/main/0205_CONTRIBUTING_publication_commits.csv", False)
