package dockerfile

import (
	"encoding/json"
	"testing"

	"github.com/alexei/tinyforge/internal/workload/plugin"
)

// validCfg is the smallest config that passes the non-strategy checks, so a
// test isolates the deploy_strategy behavior.
func validCfg(strategy string) json.RawMessage {
	m := map[string]any{"repo_owner": "o", "repo_name": "r", "port": 8080}
	if strategy != "" {
		m["deploy_strategy"] = strategy
	}
	b, _ := json.Marshal(m)
	return b
}

func TestValidate_Strategy(t *testing.T) {
	cases := []struct {
		strategy string
		wantErr  bool
	}{
		{"", false}, // backward-compat: no key -> valid
		{"recreate", false},
		{"blue-green", false}, // dockerfile supports blue-green
		{"rolling", true},     // reserved, not yet implemented
		{"junk", true},
	}
	for _, c := range cases {
		t.Run("strategy="+c.strategy, func(t *testing.T) {
			err := (&source{}).Validate(validCfg(c.strategy))
			if (err != nil) != c.wantErr {
				t.Fatalf("Validate(strategy=%q) err=%v, wantErr=%v", c.strategy, err, c.wantErr)
			}
		})
	}
}

func TestEffectiveStrategy_Default(t *testing.T) {
	if got := effectiveStrategy(Config{}); got != plugin.StrategyRecreate {
		t.Fatalf("empty strategy = %q, want recreate (historical default)", got)
	}
	if got := effectiveStrategy(Config{DeployStrategy: plugin.StrategyBlueGreen}); got != plugin.StrategyBlueGreen {
		t.Fatalf("explicit blue-green = %q, want blue-green", got)
	}
}
