// Copyright (C) 2026 Yota Hamada
// SPDX-License-Identifier: GPL-3.0-or-later

package cmd

import (
	"context"
	"os"
	"testing"

	"github.com/dagucloud/dagu/internal/cmn/config"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestShouldEnableProgress(t *testing.T) {
	tests := []struct {
		name           string
		quiet          bool
		envDisable     string
		expectedResult bool
	}{
		{
			name:           "ProgressEnabledWhenNotQuietAndEnvNotSet",
			quiet:          false,
			envDisable:     "",
			expectedResult: true, // Would be true if terminal check passes
		},
		{
			name:           "ProgressDisabledWhenQuiet",
			quiet:          true,
			envDisable:     "",
			expectedResult: false,
		},
		{
			name:           "ProgressDisabledWhenEnvVariableSet",
			quiet:          false,
			envDisable:     "1",
			expectedResult: false,
		},
		{
			name:           "ProgressDisabledWhenBothQuietAndEnvSet",
			quiet:          true,
			envDisable:     "1",
			expectedResult: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Save and restore env
			oldEnv := os.Getenv("DISABLE_PROGRESS")
			defer os.Setenv("DISABLE_PROGRESS", oldEnv)

			os.Setenv("DISABLE_PROGRESS", tt.envDisable)

			// Note: We can't easily test the terminal check in unit tests
			// The actual shouldEnableProgress also checks isTerminal(os.Stderr)
			// So we're mainly testing the quiet and env variable logic here

			// For a more complete test, we'd need to mock isTerminal
			// but that would require refactoring the production code

			// Just verify the function exists and can be called
			_ = &Context{
				Context: context.Background(),
				Quiet:   tt.quiet,
			}
		})
	}
}

func TestConfigureLoggerForProgress(t *testing.T) {
	// Create a temporary log file
	tmpFile, err := os.CreateTemp("", "test-log-*.log")
	require.NoError(t, err)
	defer os.Remove(tmpFile.Name())
	defer tmpFile.Close()

	tests := []struct {
		name      string
		debug     bool
		logFormat string
	}{
		{
			name:      "BasicConfiguration",
			debug:     false,
			logFormat: "",
		},
		{
			name:      "DebugEnabled",
			debug:     true,
			logFormat: "",
		},
		{
			name:      "WithLogFormat",
			debug:     false,
			logFormat: "json",
		},
		{
			name:      "DebugAndLogFormat",
			debug:     true,
			logFormat: "json",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ctx := &Context{
				Context: context.Background(),
				Config: &config.Config{
					Core: config.Core{
						Debug:     tt.debug,
						LogFormat: tt.logFormat,
					},
				},
			}

			// Configure logger for progress
			configureLoggerForProgress(ctx, tmpFile)

			// The logger should be configured with quiet mode
			// We can't easily test the internal logger state,
			// but we can verify the function runs without error
			assert.NotNil(t, ctx.Context)
		})
	}
}

// Additional tests could be added here to test ExecuteAgent
// but would require significant mocking of the agent and its dependencies
