// 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"
	"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
}

// 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")

	if aePath == "" {
		return mockRender(ctx, job, outputPath, onProgress, onPreview)
	}
	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
}

// ── 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>
	//   -output  <output.mp4>
	args := []string{
		"-project", job.AEPFilePath,
		"-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)
			}
			_ = onProgress(ctx, 95, "Encoding complete")
			return outputPath, 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()
		}
	}
}
