# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -*- coding: utf-8 -*-

import os
import re
import csv

import generate_task_summaries

emails_by_task = {}
email_to_name = {}
DO_NOT_INCLUDE = ["bigbench@googlegroups.com"]


def get_name_for_email(line, email):
    # Match: My Name (myemail@gmail.com)
    match = re.search(r"([\w\s\.,<>/-]+)\s+\(?\[?'?`?" + re.escape(email), line)

    if not match:
        # Match: [Kevin Gimpel](https://home.ttic.edu/~kgimpel/) (kgimpel@ttic.edu)
        match = re.search(r"\[([\w\s\.,<>/-]+)\]\([^)]+\)\s+\(?'?`?" + re.escape(email), line)

    if match:
        name = match.group(1).strip()
        assert not ":" in name
        for i in range(3):
            # Do this a few times to deal with Oxford comma
            name = re.sub(r"^(and |, )", r"", name)
        name = re.sub(r"^\s*-\s*", r"", name)
        name = re.sub(r"\s*[,-]\s*$", r"", name)
        name = re.sub(r"<sup>.*</sup>", r"", name)
        return name
    else:
        return None


try:
    os.chdir(generate_task_summaries.bench_dir)
except FileNotFoundError:
    raise ValueError(f"Cannot chdir to {generate_task_summaries.bench_dir}")


for root, dirs, files in os.walk("."):
    dirs.sort()
    for fname in files:
        if fname == "README.md":
            path = os.path.join(root, fname)

            components = path.split("/")
            assert components[0] == "."
            task = components[1]

            with open(path, "r", encoding="utf-8") as f:
                in_authors_section = False
                
                for line in f:
                    authors_match = re.search(r"Authors?|Contributors?", line, re.IGNORECASE)
                    emails_match = re.findall(r'[\w.+-]+@[\w-]+\.[\w.-]+', line)
                    whitespace_match = re.match(r"^\s*$", line)

                    if authors_match:
                        in_authors_section = True
                    elif in_authors_section:
                        if emails_match or whitespace_match:
                            # Sometimes we see "Authors:" and then emails span
                            # several lines.  Stay in authors section as long as we
                            # keep seeing emails.
                            pass
                        elif task in emails_by_task and len(emails_by_task[task]) > 0:
                            # Stop after we're done with one authors section.
                            # Make sure we found some emails, rather than just
                            # saw the word "authors" somewhere.
                            break
                        else:
                            # We saw "authors" but found no emails, so keep
                            # looking.
                            in_authors_section = False

                    if in_authors_section and emails_match:
                        if not task in emails_by_task:
                            emails_by_task[task] = list()
                        for email in emails_match:
                            if email in DO_NOT_INCLUDE:
                                continue
                            if not email in emails_by_task[task]:
                                emails_by_task[task].append(email)
                            name = get_name_for_email(line, email)
                            if name:
                                email_to_name[email] = name
                            else:
                                print(f"WARNING: Name not found for email {email}\n"
                                      f"LINE: {line.strip()}\n"
                                      f"FILE: {path}\n")
                    elif emails_match:
                        print(f"WARNING: Email found in non-authors section, skipping.\n"
                              f"LINE: {line.strip()}\n"
                              f"FILE: {path}\n")


# Save the results
tsv_filename = "task_authors.tsv"
with open(tsv_filename, "w", encoding="utf-8") as tsv_file:
    writer = csv.DictWriter(
        tsv_file,
        delimiter="\t",
        fieldnames=["task", "email", "name"])
    writer.writeheader()

    for task in sorted(emails_by_task):
        for email in emails_by_task[task]:
            try:
                name = email_to_name[email]
            except KeyError:
                name = ""
            writer.writerow({"task": task, "email": email, "name": name})


print(f"Results saved to {os.path.join(os.getcwd(), tsv_filename)}\n")
