package ui

import (
	"fmt"
	"strings"

	"bdev/internal/patch"
)

func RenderPullResult(r *patch.PullResult) string {
	var b strings.Builder

	b.WriteString(TitleStyle.Render("bdev pull"))
	b.WriteString("\n\n")

	for _, f := range r.Applied {
		b.WriteString(fmt.Sprintf("  %s %s\n", SuccessStyle.Render("+"), f))
	}
	for _, c := range r.Conflicts {
		b.WriteString(fmt.Sprintf("  %s %s\n", ErrorStyle.Render("x"), c.File))
	}
	for _, f := range r.Deleted {
		b.WriteString(fmt.Sprintf("  %s %s\n", DeletedPrefix, f))
	}
	for _, f := range r.Reverted {
		b.WriteString(fmt.Sprintf("  %s %s %s\n", ModifiedPrefix, f, MutedStyle.Render("(reverted to base)")))
	}
	for _, f := range r.LocalOnly {
		b.WriteString(fmt.Sprintf("  %s %s %s\n", SkippedPrefix, f, MutedStyle.Render("(local-only, kept)")))
	}
	if len(r.Skipped) > 0 {
		b.WriteString(fmt.Sprintf("  %s %s\n", SkippedPrefix,
			MutedStyle.Render(fmt.Sprintf("%d files skipped (already up to date)", len(r.Skipped)))))
	}

	b.WriteString("\n")

	total := len(r.Applied) + len(r.Deleted) + len(r.Reverted) + len(r.LocalOnly) + len(r.Conflicts) + len(r.Skipped)
	summary := fmt.Sprintf("Pulled %d patch paths", total)
	b.WriteString(SuccessStyle.Render(summary))
	b.WriteString(MutedStyle.Render(fmt.Sprintf(" (%d applied, %d deleted, %d reverted, %d local-only, %d conflicts, %d skipped)",
		len(r.Applied), len(r.Deleted), len(r.Reverted), len(r.LocalOnly), len(r.Conflicts), len(r.Skipped))))
	b.WriteString("\n")

	return b.String()
}
