import datetime
from git import Repo
import os
import json
import click

from elasticsearch import Elasticsearch


class GitLogger:
    """to provide a log as dict of commits which are json printable"""

    def __init__(self, path):
        """Create a GitStepper with the path to the git repository (not a bare repository)"""
        self.repo = Repo(path)

    def log(self):
        """return a dict of commits"""
        commits = (self.repo.commit(logEntry) for logEntry in self.repo.iter_commits())
        return (self.to_dict(x) for x in commits)

    def to_dict(self,commit):
        """create a dict out of a commit that is easy to json serialize"""
        time_difference = commit.authored_datetime - commit.committed_datetime
        return {
            "author_email" : commit.author.email,
            "author_name" : commit.author.name,
            "authored_date" : commit.authored_datetime.isoformat(),
            "files": [{"file":file,"changes":changes} for file,changes in commit.stats.files.items()],
            "committed_date" : commit.committed_datetime.isoformat(),
            "committer_email" : commit.committer.email,
            "committer_name" : commit.committer.name,
            "date_difference_in_seconds" : abs( time_difference.total_seconds() ),
            "encoding" : commit.encoding,
            "hash" : commit.hexsha,
            "message" : commit.message ,
            "summary" : commit.summary,
            "size" : commit.size,
            "stats_total" : commit.stats.total,
            "parents" : [parent.hexsha for parent in commit.parents],
        }

@click.command()
@click.argument("path", type=click.Path(exists=True), envvar='PWD')
@click.option("--host")
@click.option("--index")
@click.option("--port", default=9200)
def main(path, host, index, port):
    if host:
        if not index:
            print("--index need to be set")
            exit(1)

        print("Sending commits from %s to %s on index %s" % (path, host, index))
        es = Elasticsearch([
            {'host': host, 'port': port}
        ])
        for entry in GitLogger(path).log():
            try:
                es.index(
                    index=index,
                    doc_type="commit",
                    id=entry["hash"],
                    body=entry
                )
            except Exception as e:
                print(json.dumps(entry))
                print(e.info)
                exit(1)
    else:
        for entry in GitLogger(path).log():
            print(json.dumps( entry ))

if __name__ == '__main__':
    main()