package action

import (
	"context"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestNotifyStatusActivity(t *testing.T) {
	// Test with default channel (logs only)
	output, err := NotifyStatusActivity(context.Background(), NotifyStatusInput{
		Channel: "log",
		Status:  "success",
		Message: "Test notification",
	})
	require.NoError(t, err)

	require.NotEmpty(t, output.NotificationID)
	require.NotEmpty(t, output.Timestamp)
}

func TestNotifyStatusActivity_AllStatuses(t *testing.T) {
	statuses := []string{"success", "failure", "warning"}
	
	for _, status := range statuses {
		t.Run(status, func(t *testing.T) {
			output, err := NotifyStatusActivity(context.Background(), NotifyStatusInput{
				Channel: "log",
				Status:  status,
				Message: "Test " + status,
			})
			require.NoError(t, err)
			require.NotEmpty(t, output.NotificationID)
		})
	}
}

func TestArchiveResultsActivity(t *testing.T) {
	// Create temp source file
	tmpDir, err := os.MkdirTemp("", "archive-test")
	require.NoError(t, err)
	defer os.RemoveAll(tmpDir)

	srcPath := filepath.Join(tmpDir, "report.txt")
	require.NoError(t, os.WriteFile(srcPath, []byte("test report content"), 0644))

	// Archive to local destination
	destPath := filepath.Join(tmpDir, "archive.txt")
	output, err := ArchiveResultsActivity(context.Background(), ArchiveResultsInput{
		ReportPath:  srcPath,
		Destination: destPath,
	})
	require.NoError(t, err)

	require.Equal(t, destPath, output.ArchiveURL)
	require.Greater(t, output.ArchiveSize, int64(0))

	// Verify file was copied
	content, err := os.ReadFile(destPath)
	require.NoError(t, err)
	require.Equal(t, "test report content", string(content))
}

func TestArchiveResultsActivity_NotFound(t *testing.T) {
	_, err := ArchiveResultsActivity(context.Background(), ArchiveResultsInput{
		ReportPath:  "/nonexistent/file.txt",
		Destination: "/tmp/archive.txt",
	})
	require.Error(t, err)
	require.Contains(t, err.Error(), "not found")
}

func TestDeploymentPreCheckActivity(t *testing.T) {
	// Create temp directory with valid Go code
	tmpDir, err := os.MkdirTemp("", "precheck-test")
	require.NoError(t, err)
	defer os.RemoveAll(tmpDir)

	// Create go.mod
	goMod := `module test
go 1.21
`
	require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(goMod), 0644))

	// Create valid Go file
	goCode := `package main

func main() {
	println("hello")
}
`
	require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "main.go"), []byte(goCode), 0644))

	// Run pre-check
	output, err := DeploymentPreCheckActivity(context.Background(), DeploymentPreCheckInput{
		Path:      tmpDir,
		CheckType: "build",
	})
	require.NoError(t, err)

	require.True(t, output.Passed)
	require.Empty(t, output.Failures)
}

func TestDeploymentPreCheckActivity_PathNotExist(t *testing.T) {
	output, err := DeploymentPreCheckActivity(context.Background(), DeploymentPreCheckInput{
		Path: "/nonexistent/path",
	})
	require.NoError(t, err) // Activity doesn't error, just reports failure

	require.False(t, output.Passed)
	require.NotEmpty(t, output.Failures)
}

func TestApproveWorkflowActivity(t *testing.T) {
	output, err := ApproveWorkflowActivity(context.Background(), ApproveWorkflowInput{
		WorkflowID:        "test-workflow-123",
		RequiredApprovals: 1,
		TimeoutMinutes:    60,
	})
	require.NoError(t, err)

	// Auto-approved
	require.True(t, output.Approved)
	require.Equal(t, "system-auto", output.Approver)
	require.NotEmpty(t, output.Timestamp)
}
