import requests, os

GITEA_URL = "https://git.trtmn.io"
API_TOKEN = "315d5a6d692eeea061f714725f0d116b819ea62b"
organization = "fonts"

headers = {
    "Authorization": f"token {API_TOKEN}",
    "Content-Type": "application/json"
}

def get_folders_in_folder(path="./font_files"):
    return [file for file in os.listdir(path) if os.path.isdir(os.path.join(path, file))]

folders = ["folder-1", "folder-2", "folder-3", "folder-4", "folder-5"]

# def make_local_repos(folder_list=folders):
#     for folder in folder_list:
#         # Create a local folder
#         print(f"Creating folder {folder}")
#         os.makedirs(folder, exist_ok=True)
#         # Create a README.md file in the folder
#         with open(f"{folder}/README.md", "w") as f:
#             f.write(f"# {folder}\n\nThis is a repo for {folder}")
#         #git init
#         os.system(f"cd {folder} && git init")
#         #git create branch "main" and switch to it
#         os.system(f"cd {folder} && git checkout -b main")
#         #git add README.md
#         os.system(f"cd {folder} && git add README.md")
#         #git commit -m "Initial commit"
#         os.system(f"cd {folder} && git commit -m 'Initial commit'")
#         #git remote add origin
#         os.system(f"cd {folder} && git remote add origin {GITEA_URL}/{organization}/{folder}.git")
#         #git push -u origin master
#         os.system(f"cd {folder} && git push --force -u origin main")


# def clone_repos(folder_list=folders):
#     for folder in folder_list:
#         os.system(f"git clone {GITEA_URL}/{organization}/{folder}.git")

def add_as_submodule(folder_list=folders):
    for folder in folder_list:
        os.system(f"git submodule add {GITEA_URL}/{organization}/{folder}.git")


def create_repos(folder_list=folders):
    for folder in folder_list:
        data = {
            "name": folder,
            "description": f"Repo for {folder}",
            "private": False,
            "auto_init": True
        }
        response = requests.post(f"{GITEA_URL}/api/v1/orgs/{organization}/repos", json=data, headers=headers)
        if response.status_code == 201:
            print(f"Successfully created repo for {folder}")
        else:
            print(f"Failed to create repo for {folder}: {response.status_code} {response.text}")

def delete_repos(folder_list=folders):
    for folder in folder_list:
        response = requests.delete(f"{GITEA_URL}/api/v1/repos/{organization}/{folder}", headers=headers)
        if response.status_code == 204:
            print(f"Successfully deleted repo for {folder}")
        else:
            print(f"Failed to delete repo for {folder}: {response.status_code} {response.text}")


if __name__ == '__main__':
    # create_repos()
    # make_local_repos()
    # add_as_submodule()
    # delete_repos()
    print(get_folders_in_folder())