import os
import shutil
import sys
import random

import pytest
import pytest_check as check

import skilleter_modules.git as git

def test_git(tmp_path):
    """Integration-style test suite for the git module.

    Args:
        tmp_path: Pytest temporary path fixture.

    Returns:
        None.
    """

    print('Creating local git repo')

    os.chdir(tmp_path)

    git.init('test-repo')
    os.chdir('test-repo')

    print('Initial status:')
    print('  User name:            %s' % git.config_get('user', 'name'))
    print('  Project:              %s' % git.project())
    print('  Remotes:              %s' % git.remotes())
    print('  Current branch:       %s' % git.branch())
    print('  Current working tree: %s' % git.working_tree())
    print('  Rebasing?             %s' % git.rebasing())
    print('  Status:               %s' % git.status())
    print('  Default branch?:      %s' % git.is_default_branch())
    print()

    print('Adding and removing a configuration value')

    randovalue = str(random.randint(0,999999))

    git.config_set('user', 'size', randovalue)

    value = git.config_get('user', 'size')

    check.equal(randovalue, value, f'Config value not set correctly. Should be {randovalue} but is {value}')

    # TODO: This isn't working - git.config_rm doesn't remove the config entry

    #git.config_rm('user', 'size')

    #value = git.config_get('user', 'size')

    #check.is_true(value is None, f'Unexpected lack of error reading configuration data - user.size={value}')

    git.config_set('user', 'email', 'user@localhost')
    git.config_set('user', 'name', 'User Name')

    print('')

    with open('newfile.txt', 'w', encoding='utf8') as newfile:
        newfile.write('THIS IS A TEST')

    print('Adding and committing "newfile.txt"')

    git.add(['newfile.txt'])

    git.commit(['newfile.txt'], 'Test the add and commit functionality')

    print('Removing and committing "newfile.txt"')

    git.rm(['newfile.txt'])

    git.commit(None, 'Removed "newfile.txt"')

    print('Removing the last commit')

    git.reset('HEAD~1')

    print('Commit info for HEAD  %s' % git.commit_info('HEAD'))

    print('Check tagging functions')

    randovalue = str(random.randint(0,999999))

    check.is_false(git.tag_exists(randovalue))

    git.tag_apply(randovalue)

    check.is_true(git.tag_exists(randovalue))

    #check.is_false(git.tag_exists(randovalue, remote='origin', path='/home/jms/gamstop/gamstop-dev/infrastructure/tg-aws-service-gamstop-all'))

    #check.is_true(git.tag_exists('plan-bootstrap', remote='origin', path='/home/jms/gamstop/gamstop-dev/infrastructure/tg-aws-service-gamstop-all'))

    #check.is_true(git.iscommit('feature/GT-35', remote=True, path='/home/jms/gamstop/gamstop-dev/infrastructure/tf-aws-component-bootstrap'))
    #check.is_false(git.iscommit('feature/GT-35', path='/home/jms/gamstop/gamstop-dev/infrastructure/tf-aws-component-bootstrap'))
