import datetime
import textwrap
from collections import defaultdict
from datetime import datetime

import markdown_strings as md

from src.ai_utils import summarize_commit_messages
from src.git_utils import generate_commit_hyperlink


def generate_article_content(commit_data: list[dict], months_back: int, ai_key: str, ai_model: str) -> str:
    """Generates a blog article based on commit data.
    In a real tool, this would call a large language model API (e.g., Gemini).

    Args:
        commit_data: Structured commit data.
        months_back: The number of months the analysis covered.

    Returns:
        A string containing the blog article.

    """
    if not commit_data:
        return "No relevant commits found to generate an article."

    today = datetime.now()
    article_content = f"""
# Today {today.strftime('%Y-%m-%d')} Developments: A Look at Our Codebase ({months_back} Months Review)

We're excited to share a summary of the significant progress made across our repositories
in the last {months_back} months. Our dedicated team has been busy pushing new features,
refining existing functionalities, and enhancing the overall stability of our products.

Here's a breakdown of key contributions by repository:

"""

    for repo in commit_data:

        if "error" in repo or not repo["commits"]:
            continue

        article_content += f"## {repo['repo_name']}\n\n"
        article_content += f"Repository URL: {repo['repo_url']}\n\n"

        commits_by_author = defaultdict(list)
        for commit in repo["commits"]:
            commits_by_author[commit["author_name"]].append(commit)

        if not commits_by_author:
            continue

        article_content += "### Summary of the contributions by author:\n\n"

        for author_name, author_commits in sorted(commits_by_author.items()):
            all_author_messages = "\n".join([
                    commit["message"] if commit["message"] else "(No message provided)"
                    for commit in author_commits
                ])

            if ai_key:
                ai_summary = summarize_commit_messages(ai_key, all_author_messages,
                                                       months_back, author_name, ai_model)
                ai_summary = md.esc_format(ai_summary, esc=True)
                article_content += "\n"
                if ai_summary:
                    wrapped_summary = textwrap.fill(ai_summary, width=100)
                    article_content += f"**{author_name}**: {wrapped_summary}\n\n"

            article_content += f"#### Here the commits of **{author_name}** in detail:\n\n"
            # Sort the commit by Author Name and then by date
            data = sorted(author_commits, key=lambda x: (x["author_name"], x["date"]))
            for commit in data:
                # Ensure the message is handled, even if it's empty or malformed
                first_line_message = (commit["message"].split("\n")[0] if commit["message"] else "(No message)")
                first_line_message = first_line_message.replace("_", r"\_")
                hyperlink = generate_commit_hyperlink(repo["repo_path"], repo["repo_url"], commit["sha1"])
                article_content += f"- **{commit['author_name']}** on {commit['date'].split('T')[0]}: [{first_line_message}]({hyperlink})\n"
            article_content += "\n"
    article_content += """
This overview highlights the continuous effort and innovation from our development team. We look forward to bringing even more exciting updates in the future!

---
*Generated by the Git Commit Article Generator*
"""
    return article_content
