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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::fmt::Formatter;

use colored::Colorize;
use tokio::task;

use super::prompts::{
    commit_ai, commit_body, commit_breaking, commit_confirm, commit_issue,
    commit_scope, commit_subject, commit_translate, commit_type,
};
use crate::{
    ai::{gemini::Gemini, AI},
    config,
    error::ReviseResult,
    git::GitUtils,
    revise::prompts::Inquire,
    AICommand, ReviseCommands,
};

#[derive(Debug, Default, Clone)]
pub struct Template {
    pub commit_type: commit_type::Part,
    pub commit_scope: commit_scope::Part,
    pub commit_subject: commit_subject::Part,
    pub commit_body: commit_body::Part,
    pub commit_breaking: commit_breaking::Part,
    pub commit_issue: commit_issue::Part,
}

impl Template {
    pub async fn run(
        &mut self,
        cmd: &Option<ReviseCommands>,
    ) -> ReviseResult<String> {
        if let Some(a) = cmd {
            self.run_action(&a.ai).await?;
        } else {
            self.run_default()?;
        }
        let mut confirm = commit_confirm::Part::new(self.clone());
        confirm.inquire()?;
        Ok(confirm.ans.unwrap())
    }

    pub fn run_default(&mut self) -> ReviseResult<()> {
        self.commit_type.inquire()?;
        self.commit_scope.inquire()?;
        self.commit_subject.inquire()?;
        self.commit_body.inquire()?;
        self.commit_breaking.inquire()?;
        self.commit_issue.inquire()?;
        Ok(())
    }

    pub async fn run_action(&mut self, cmd: &AICommand) -> ReviseResult<()> {
        let cfg = config::get_config();
        let Some(key) = cfg.api_key.get("gemini_key") else {
            return Err(anyhow::anyhow!("API key not found"));
        };

        let gemini = Gemini::new(key);
        let mut s = match cmd {
            AICommand::Translate(s) => s.to_string(),
            AICommand::Generate => GitUtils::new().diff()?,
        };
        if s.is_empty() {
            let mut translate = commit_translate::Part::new();
            translate.inquire()?;
            s = match translate.ans {
                Some(s) => s,
                None => {
                    return Err(anyhow::anyhow!("Translate message is empty"));
                }
            };
        }
        let handle =
            task::spawn(async move { gemini.generate_response(&s).await });

        self.commit_type.inquire()?;
        self.commit_scope.inquire()?;
        self.commit_breaking.inquire()?;
        self.commit_issue.inquire()?;

        let res = handle.await??;

        let mut ai = commit_ai::Part::new(res.keys().cloned().collect());
        ai.inquire()?;
        let ai_ans = res.get(&ai.ans.clone().unwrap()).unwrap();
        self.commit_subject.ans = Some(ai_ans.message.clone());
        self.commit_body.ans = Some(ai_ans.body.clone());
        Ok(())
    }

    pub fn get_ctype(&self) -> String {
        self.commit_type.ans.clone().unwrap()
    }
    pub fn get_cscope(&self) -> Option<String> {
        self.commit_scope.ans.clone()
    }
    pub fn get_csubject(&self) -> String {
        self.commit_subject.ans.clone().unwrap()
    }
    pub fn get_cbody(&self) -> Option<String> {
        self.commit_body.ans.clone()
    }
    pub fn get_cbreaking(&self) -> Option<String> {
        self.commit_breaking.ans.clone()
    }
    pub fn get_cissue(&self) -> Option<String> {
        self.commit_issue.ans.clone()
    }

    pub fn show(&self) -> String {
        let mut msg = String::new();

        String::push_str(&mut msg, &format!("{}", &self.get_ctype().green()));

        match (&self.get_cscope(), &self.get_cbreaking()) {
            (None, None) => {
                msg.push_str(": ");
            }
            (None, Some(_)) => {
                let scope = format!("{}: ", "!".bright_red());
                msg.push_str(&scope);
            }
            (Some(scope), None) => {
                let scope = format!("({}): ", scope.yellow());
                msg.push_str(&scope);
            }
            (Some(scope), Some(_)) => {
                let scope =
                    format!("({}){}: ", scope.yellow(), "!".bright_red());
                msg.push_str(&scope);
            }
        }

        let subject = format!("{}", &self.get_csubject().bright_cyan());
        msg.push_str(&subject);

        match &self.get_cissue() {
            Some(issues) => {
                let issues = format!("({})", issues.blue());
                msg.push_str(&issues);
            }
            None => {}
        }
        match &self.get_cbody() {
            Some(body) => {
                let body = format!("\n{body}");
                msg.push_str(&body);
            }
            None => {}
        }
        match &self.get_cbreaking() {
            Some(breaking) => {
                let breaking =
                    format!("\n\n{}: {}", "BREAKING CHANGE".red(), breaking);
                msg.push_str(&breaking);
            }
            None => {}
        }
        msg
    }
}

impl std::fmt::Display for Template {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let mut msg = String::new();

        // let ctype = &self.commit_type;
        msg.push_str(&self.get_ctype());

        match (&self.get_cscope(), &self.get_cbreaking()) {
            (None, None) => {
                msg.push_str(": ");
            }
            (None, Some(_)) => {
                let scope = format!("{}: ", "!");
                msg.push_str(&scope);
            }
            (Some(scope), None) => {
                let scope = format!("({scope}): ");
                msg.push_str(&scope);
            }
            (Some(scope), Some(_)) => {
                let scope = format!("({}){}: ", scope, "!");
                msg.push_str(&scope);
            }
        }

        // let subject = &self.commit_subject;
        msg.push_str(&self.get_csubject());

        match &self.get_cissue() {
            Some(issues) => {
                let issues = format!("({issues})");
                msg.push_str(&issues);
            }
            None => {}
        }
        match &self.get_cbody() {
            Some(body) => {
                let body = format!("\n{body}");
                msg.push_str(&body);
            }
            None => {}
        }
        match &self.get_cbreaking() {
            Some(breaking) => {
                let breaking =
                    format!("\n\n{}: {}", "BREAKING CHANGE", breaking);
                msg.push_str(&breaking);
            }
            None => {}
        }
        write!(f, "{msg}")
    }
}