// Package runner executes After Effects render jobs and streams progress back
// via the provided callbacks. When AE_PATH is empty, a mock render is used
// (useful for CI and dev environments without a licensed AE installation).
package runner

import (
	"context"
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
	"time"
)

// ProgressFn is called periodically during rendering with (percent 0-100, message).
type ProgressFn func(ctx context.Context, percent int, message string) error

// PreviewFn is called each time a new preview frame is ready.
// The argument is a base64-encoded PNG. Errors are non-fatal.
type PreviewFn func(ctx context.Context, imageB64 string) error

// Job holds the parameters for a single render.
type Job struct {
	JobID          string
	SavedProjectID string
	Quality        string
	Resolution     string
	FrameRate      int
	HasMusic       bool
	HasVoiceover   bool
	// AEPFilePath is the local path to the downloaded .aep project file.
	// In a full implementation the agent downloads this from MinIO before calling Run.
	AEPFilePath string
	// CompName is the composition to render (-comp), e.g. "frfinal". When empty the
	// node renders the project's render queue (-rqindex 1) instead.
	CompName string
	// AfterFxPath is afterfx.exe (the AE app, scriptable) — used by the binder to write
	// input values into the project before rendering. Render itself uses aerender.exe.
	AfterFxPath string
	// Bindings are the user's edited input values to write into the .aep before render.
	Bindings []Binding
}

// Binding is one input value written into the AE project before render.
type Binding struct {
	Key   string // AE layer/footage name, e.g. frl_c1t1 / frl_c1m1
	Type  string // content element type (Text, Media, …)
	Value string // text content, or a media URL
}

// Run executes the render job, calling onProgress and onPreview as it advances.
// Returns the path to the output MP4 file on success.
func Run(ctx context.Context, aePath, workDir string, job *Job, onProgress ProgressFn, onPreview PreviewFn) (string, error) {
	outputDir := filepath.Join(workDir, "renders", job.JobID)
	if err := os.MkdirAll(outputDir, 0o755); err != nil {
		return "", fmt.Errorf("create output dir: %w", err)
	}
	outputPath := filepath.Join(outputDir, "output.mp4")

	// Mock render when AE isn't installed (aePath empty) OR when this job has no
	// template project to render (AEPFilePath empty — the template bundle wasn't
	// uploaded/promoted yet). Mock drives progress+preview to completion so the job
	// doesn't hard-fail; a real render requires both AE and a downloaded .aep.
	if aePath == "" || job.AEPFilePath == "" {
		if aePath != "" && job.AEPFilePath == "" {
			log.Printf("[job %s] no template .aep available — falling back to mock render", job.JobID)
		}
		return mockRender(ctx, job, outputPath, onProgress, onPreview)
	}

	// Render binder: write the user's edited input values into the project before
	// rendering so the MP4 reflects their text/media. Non-fatal — on failure we render
	// the template defaults rather than failing the job.
	if len(job.Bindings) > 0 && job.AfterFxPath != "" {
		_ = onProgress(ctx, 6, "Applying your edits…")
		bound, berr := RunBinder(ctx, job, workDir)
		if berr != nil {
			log.Printf("[job %s] binder failed (%v) — rendering template defaults", job.JobID, berr)
		} else {
			job.AEPFilePath = bound // render the bound project with the user's values
		}
	}

	return aeRender(ctx, aePath, job, outputPath, onProgress, onPreview)
}

// ── Mock render (no AE installed) ────────────────────────────────────────────

func mockRender(ctx context.Context, job *Job, outputPath string, onProgress ProgressFn, onPreview PreviewFn) (string, error) {
	log.Printf("[mock] starting render for job %s (%s %s %dfps)", job.JobID, job.Quality, job.Resolution, job.FrameRate)

	steps := []struct {
		pct int
		msg string
	}{
		{5, "Preparing project…"},
		{15, "Loading template…"},
		{30, "Rendering frames…"},
		{50, "Rendering frames… (50%)"},
		{70, "Rendering frames… (70%)"},
		{85, "Encoding MP4…"},
		{95, "Uploading output…"},
	}

	for _, s := range steps {
		select {
		case <-ctx.Done():
			return "", ctx.Err()
		case <-time.After(800 * time.Millisecond):
		}
		if err := onProgress(ctx, s.pct, s.msg); err != nil {
			log.Printf("[mock] progress callback error: %v", err)
		}
		// Generate and push a preview frame at each step
		if onPreview != nil {
			b64 := GeneratePreviewB64(s.pct, job.Quality, job.Resolution)
			if err := onPreview(ctx, b64); err != nil {
				log.Printf("[mock] preview callback error: %v", err)
			}
		}
		log.Printf("[mock] %d%% — %s", s.pct, s.msg)
	}

	// Write a placeholder file so the path is valid
	if err := os.WriteFile(outputPath, []byte("mock-render-output"), 0o644); err != nil {
		return "", fmt.Errorf("write mock output: %w", err)
	}
	log.Printf("[mock] render complete: %s", outputPath)
	return outputPath, nil
}

// findRenderedOutput locates the file aerender actually produced. The requested
// path is e.g. <dir>/output.mp4, but the output module may have written
// output.avi / output.mov / output.mp4. Prefer an exact match, then .mp4, then
// the largest output.* file in the directory.
func findRenderedOutput(requested string) string {
	if st, err := os.Stat(requested); err == nil && st.Size() > 0 {
		return requested
	}
	dir := filepath.Dir(requested)
	base := strings.TrimSuffix(filepath.Base(requested), filepath.Ext(requested)) // "output"
	matches, _ := filepath.Glob(filepath.Join(dir, base+".*"))
	var best string
	var bestSize int64 = -1
	for _, m := range matches {
		st, err := os.Stat(m)
		if err != nil || st.IsDir() {
			continue
		}
		// Prefer .mp4 immediately.
		if strings.EqualFold(filepath.Ext(m), ".mp4") && st.Size() > 0 {
			return m
		}
		if st.Size() > bestSize {
			best, bestSize = m, st.Size()
		}
	}
	return best
}

// ffmpegPath locates an ffmpeg binary: $FFMPEG_PATH, then `ffmpeg(.exe)` next to
// the agent executable, then PATH. Returns "" when none is found.
func ffmpegPath() string {
	if p := os.Getenv("FFMPEG_PATH"); p != "" {
		if _, err := os.Stat(p); err == nil {
			return p
		}
	}
	name := "ffmpeg"
	if runtime.GOOS == "windows" {
		name = "ffmpeg.exe"
	}
	if exe, err := os.Executable(); err == nil {
		cand := filepath.Join(filepath.Dir(exe), name)
		if _, err := os.Stat(cand); err == nil {
			return cand
		}
	}
	if p, err := exec.LookPath(name); err == nil {
		return p
	}
	return ""
}

// transcodeToMP4 converts a lossless AE render (AVI/MOV) to a web-playable H.264
// MP4 using ffmpeg. Returns the .mp4 path. Errors if ffmpeg is unavailable.
// resolutionHeight maps a quality-tier label to its output height (mirrors render-svc).
func resolutionHeight(resolution string) int {
	switch strings.ToLower(strings.TrimSpace(resolution)) {
	case "360p":
		return 360
	case "540p":
		return 540
	case "720p":
		return 720
	case "1080p", "fullhd":
		return 1080
	case "4k", "2160p":
		return 2160
	default:
		return 0 // unknown → no scaling
	}
}

func transcodeToMP4(ctx context.Context, src, requested string, height int) (string, error) {
	ff := ffmpegPath()
	if ff == "" {
		return "", fmt.Errorf("ffmpeg not found (set FFMPEG_PATH or place ffmpeg.exe next to the agent)")
	}
	dst := strings.TrimSuffix(requested, filepath.Ext(requested)) + ".mp4"
	args := []string{
		"-y", "-i", src,
		"-c:v", "libx264", "-preset", "medium", "-crf", "20", "-pix_fmt", "yuv420p",
	}
	// Downscale (or up) to the selected quality tier. -2 keeps width even & aspect.
	if height > 0 {
		args = append(args, "-vf", fmt.Sprintf("scale=-2:%d", height))
	}
	args = append(args,
		"-c:a", "aac", "-b:a", "192k",
		"-movflags", "+faststart",
		dst,
	)
	log.Printf("[ffmpeg] %s %v", ff, args)
	cmd := exec.CommandContext(ctx, ff, args...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		return "", fmt.Errorf("ffmpeg: %w", err)
	}
	if st, err := os.Stat(dst); err != nil || st.Size() == 0 {
		return "", fmt.Errorf("ffmpeg produced no output")
	}
	return dst, nil
}

// ── Real AE render via aerender.exe ──────────────────────────────────────────

func aeRender(ctx context.Context, aePath string, job *Job, outputPath string, onProgress ProgressFn, onPreview PreviewFn) (string, error) {
	if job.AEPFilePath == "" {
		return "", fmt.Errorf("AEPFilePath is required for real AE render")
	}

	// aerender flags:
	//   -project <path.aep>
	//   -comp <name>  (or -rqindex 1 when no comp name is known)
	//   -output  <output.mp4>
	// Modern AE can't reliably write H.264 directly from aerender, so we let it
	// render with the project's output module (typically Lossless AVI/MOV) and
	// transcode to MP4 with ffmpeg afterwards (see transcodeToMP4).
	// Without -comp/-rqindex, aerender ignores -output and renders nothing.
	args := []string{"-project", job.AEPFilePath}
	if job.CompName != "" {
		args = append(args, "-comp", job.CompName)
	} else {
		args = append(args, "-rqindex", "1")
	}
	args = append(args, "-output", outputPath)

	log.Printf("[ae] running: %s %v", aePath, args)
	cmd := exec.CommandContext(ctx, aePath, args...)
	// Run from the project's folder so a .zip bundle's relative footage/font paths
	// resolve correctly (the .aep sits alongside its assets after extraction).
	cmd.Dir = filepath.Dir(job.AEPFilePath)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	if err := cmd.Start(); err != nil {
		return "", fmt.Errorf("start aerender: %w", err)
	}

	// Poll process while alive — aerender does not expose machine-readable progress.
	// We advance the progress indicator every 10 seconds until the process exits.
	done := make(chan error, 1)
	go func() { done <- cmd.Wait() }()

	_ = onProgress(ctx, 10, "After Effects starting…")
	pct := 10
	ticker := time.NewTicker(10 * time.Second)
	defer ticker.Stop()

	// Generate preview frames every 30 seconds during real AE render.
	// In a full implementation this would screenshot the AE composition output.
	previewTicker := time.NewTicker(30 * time.Second)
	defer previewTicker.Stop()

	for {
		select {
		case err := <-done:
			if err != nil {
				return "", fmt.Errorf("aerender exit: %w", err)
			}
			// Find what aerender actually wrote (output.avi / .mov / .mp4).
			actual := findRenderedOutput(outputPath)
			if actual == "" {
				return "", fmt.Errorf("aerender finished but no output file found in %s", filepath.Dir(outputPath))
			}
			// Already an MP4? done.
			if strings.EqualFold(filepath.Ext(actual), ".mp4") {
				_ = onProgress(ctx, 95, "Encoding complete")
				return actual, nil
			}
			// Transcode the lossless render → H.264 MP4 (much smaller, web-playable).
			_ = onProgress(ctx, 92, "Transcoding to MP4…")
			mp4, terr := transcodeToMP4(ctx, actual, outputPath, resolutionHeight(job.Resolution))
			if terr != nil {
				// ffmpeg missing/failed — fall back to the raw render so the job
				// still delivers a file (large, but valid).
				log.Printf("[ae] transcode failed (%v) — uploading raw %s", terr, filepath.Ext(actual))
				return actual, nil
			}
			_ = onProgress(ctx, 95, "Encoding complete")
			_ = os.Remove(actual) // drop the multi-GB intermediate
			return mp4, nil
		case <-ticker.C:
			if pct < 90 {
				pct += 5
			}
			_ = onProgress(ctx, pct, fmt.Sprintf("Rendering… %d%%", pct))
		case <-previewTicker.C:
			if onPreview != nil {
				b64 := GeneratePreviewB64(pct, job.Quality, job.Resolution)
				if err := onPreview(ctx, b64); err != nil {
					log.Printf("[ae] preview push error: %v", err)
				}
			}
		case <-ctx.Done():
			_ = cmd.Process.Kill()
			return "", ctx.Err()
		}
	}
}
