1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use super::common::generate_changes_content;
use super::models::{
    BreakingChange, ChangeMetrics, Highlight, ReleaseNotesResponse, Section, SectionItem,
};
use super::prompt;
use crate::common::DetailLevel;
use crate::config::Config;
use anyhow::Result;
use colored::*;
use std::path::Path;

/// Struct responsible for generating release notes
pub struct ReleaseNotesGenerator;

impl ReleaseNotesGenerator {
    /// Generates release notes for the specified range of commits.
    ///
    /// # Arguments
    ///
    /// * `repo_path` - Path to the Git repository
    /// * `from` - Starting point for the release notes (e.g., a commit hash or tag)
    /// * `to` - Ending point for the release notes (e.g., a commit hash, tag, or "HEAD")
    /// * `config` - Configuration object containing LLM settings
    /// * `detail_level` - Level of detail for the release notes (Minimal, Standard, or Detailed)
    ///
    /// # Returns
    ///
    /// A Result containing the generated release notes as a String, or an error
    pub async fn generate(
        repo_path: &Path,
        from: &str,
        to: &str,
        config: &Config,
        detail_level: DetailLevel,
    ) -> Result<String> {
        let release_notes: ReleaseNotesResponse = generate_changes_content::<ReleaseNotesResponse>(
            repo_path,
            from,
            to,
            config,
            detail_level,
            prompt::create_release_notes_system_prompt,
            prompt::create_release_notes_user_prompt,
        )
        .await?;

        Ok(format_release_notes_response(&release_notes))
    }
}

/// Formats the ReleaseNotesResponse into human-readable release notes
fn format_release_notes_response(response: &ReleaseNotesResponse) -> String {
    let mut formatted = String::new();

    // Add header
    formatted.push_str(&format!(
        "# Release Notes - v{}\n\n",
        response
            .version
            .clone()
            .unwrap_or_default()
            .bright_green()
            .bold()
    ));
    formatted.push_str(&format!(
        "Release Date: {}\n\n",
        response.release_date.clone().unwrap_or_default().yellow()
    ));

    // Add summary
    formatted.push_str(&format!("{}\n\n", response.summary.bright_cyan()));

    // Add highlights
    if !response.highlights.is_empty() {
        formatted.push_str(&"## ✨ Highlights\n\n".bright_magenta().bold().to_string());
        for highlight in &response.highlights {
            formatted.push_str(&format_highlight(highlight));
        }
    }

    // Add changes grouped by section
    for section in &response.sections {
        formatted.push_str(&format_section(section));
    }

    // Add breaking changes
    if !response.breaking_changes.is_empty() {
        formatted.push_str(&"## ⚠️ Breaking Changes\n\n".bright_red().bold().to_string());
        for breaking_change in &response.breaking_changes {
            formatted.push_str(&format_breaking_change(breaking_change));
        }
    }

    // Add upgrade notes
    if !response.upgrade_notes.is_empty() {
        formatted.push_str(&"## 🔧 Upgrade Notes\n\n".yellow().bold().to_string());
        for note in &response.upgrade_notes {
            formatted.push_str(&format!("- {}\n", note));
        }
        formatted.push('\n');
    }

    // Add metrics
    formatted.push_str(&"## 📊 Metrics\n\n".bright_blue().bold().to_string());
    formatted.push_str(&format_metrics(&response.metrics));

    formatted
}

/// Formats a highlight
fn format_highlight(highlight: &Highlight) -> String {
    format!(
        "### {}\n\n{}\n\n",
        highlight.title.bright_yellow().bold(),
        highlight.description
    )
}

/// Formats a section
fn format_section(section: &Section) -> String {
    let mut formatted = format!("## {}\n\n", section.title.bright_blue().bold());
    for item in &section.items {
        formatted.push_str(&format_section_item(item));
    }
    formatted.push('\n');
    formatted
}

/// Formats a section item
fn format_section_item(item: &SectionItem) -> String {
    let mut formatted = format!("- {}", item.description);

    if !item.associated_issues.is_empty() {
        formatted.push_str(&format!(
            " ({})",
            item.associated_issues.join(", ").yellow()
        ));
    }

    if let Some(pr) = &item.pull_request {
        formatted.push_str(&format!(" [{}]", pr.bright_purple()));
    }

    formatted.push('\n');
    formatted
}

/// Formats a breaking change
fn format_breaking_change(breaking_change: &BreakingChange) -> String {
    format!(
        "- {} ({})\n",
        breaking_change.description,
        breaking_change.commit_hash.dimmed()
    )
}

/// Formats the change metrics
fn format_metrics(metrics: &ChangeMetrics) -> String {
    format!(
        "- Total Commits: {}\n- Files Changed: {}\n- Insertions: {}\n- Deletions: {}\n",
        metrics.total_commits.to_string().green(),
        metrics.files_changed.to_string().yellow(),
        metrics.insertions.to_string().green(),
        metrics.deletions.to_string().red()
    )
}