//! Pure merge-status message formatting for `service::merge`.
//!
//! These functions transform a completed merge report into human-readable
//! status messages. No I/O: they are pure functions over plain data.

use crate::agents::merge::MergeReport;

#[allow(dead_code)]
/// Derive a human-readable status message from a completed [`MergeReport`].
///
/// The message explains what happened and (on failure) what the caller
/// should do next.
pub fn format_merge_status_message(report: &MergeReport) -> &'static str {
    if report.success && report.gates_passed && report.conflicts_resolved {
        "Merge complete: conflicts were auto-resolved and all quality gates passed. Story moved to done and worktree cleaned up."
    } else if report.success && report.gates_passed {
        "Merge complete: all quality gates passed. Story moved to done and worktree cleaned up."
    } else if report.had_conflicts && !report.conflicts_resolved {
        "Merge failed: conflicts detected that could not be auto-resolved. Merge was aborted — master is untouched. Call report_merge_failure with the conflict details so the human can resolve them. Do NOT manually move the story file or call accept_story."
    } else if report.success && !report.gates_passed {
        "Merge committed but quality gates failed. Review gate_output and fix issues before re-running."
    } else {
        "Merge failed. Review gate_output for details. Call report_merge_failure to record the failure. Do NOT manually move the story file or call accept_story."
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn report(
        success: bool,
        had_conflicts: bool,
        conflicts_resolved: bool,
        gates_passed: bool,
    ) -> MergeReport {
        MergeReport {
            story_id: String::new(),
            success,
            had_conflicts,
            conflicts_resolved,
            conflict_details: None,
            gates_passed,
            gate_output: String::new(),
            worktree_cleaned_up: false,
            story_archived: false,
        }
    }

    #[test]
    fn clean_merge_message() {
        let r = report(true, false, false, true);
        let msg = format_merge_status_message(&r);
        assert!(msg.contains("quality gates passed"));
        assert!(msg.contains("done"));
    }

    #[test]
    fn conflicts_resolved_message() {
        let r = report(true, true, true, true);
        let msg = format_merge_status_message(&r);
        assert!(msg.contains("auto-resolved"));
    }

    #[test]
    fn unresolved_conflicts_message() {
        let r = report(false, true, false, false);
        let msg = format_merge_status_message(&r);
        assert!(msg.contains("could not be auto-resolved"));
        assert!(msg.contains("report_merge_failure"));
    }

    #[test]
    fn gates_failed_message() {
        let r = report(true, false, false, false);
        let msg = format_merge_status_message(&r);
        assert!(msg.contains("quality gates failed"));
    }

    #[test]
    fn general_failure_message() {
        let r = report(false, false, false, false);
        let msg = format_merge_status_message(&r);
        assert!(msg.contains("Merge failed"));
        assert!(msg.contains("report_merge_failure"));
    }
}
