package exec_test

import (
	"encoding/json"
	"testing"

	"github.com/mathiasbq/supervisor/internal/exec"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestResultParsesValidJSON(t *testing.T) {
	raw := `{
		"status": "pass",
		"phase": "red",
		"skill": "tdd",
		"file_path": "/tmp/foo_test.go",
		"runner_output": "--- FAIL: TestFoo",
		"verified": true,
		"model_used": "self",
		"message": "test fails as expected"
	}`
	var r exec.Result
	require.NoError(t, json.Unmarshal([]byte(raw), &r))
	assert.Equal(t, "pass", r.Status)
	assert.Equal(t, "red", r.Phase)
	assert.True(t, r.Verified)
}

func TestResultValidation(t *testing.T) {
	tests := []struct {
		name    string
		result  exec.Result
		wantErr bool
	}{
		{
			name: "valid pass result",
			result: exec.Result{
				Status: "pass", Phase: "red", Skill: "tdd",
				FilePath: "/tmp/x_test.go", RunnerOutput: "FAIL",
				Verified: true, ModelUsed: "self", Message: "ok",
			},
			wantErr: false,
		},
		{
			name:    "empty status",
			result:  exec.Result{Phase: "red", Skill: "tdd"},
			wantErr: true,
		},
		{
			name:    "invalid status",
			result:  exec.Result{Status: "unknown", Phase: "red", Skill: "tdd"},
			wantErr: true,
		},
		{
			name:    "invalid phase",
			result:  exec.Result{Status: "pass", Phase: "bad", Skill: "tdd"},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := tt.result.Validate()
			if tt.wantErr {
				assert.Error(t, err)
			} else {
				assert.NoError(t, err)
			}
		})
	}
}
