// file is generated by AI
package cloudintegrationtypes

import (
	"fmt"
	"strings"
	"testing"

	"github.com/SigNoz/signoz/pkg/valuer"
)

var testTemplateData = azureTemplateData{
	TemplateURL:             fmt.Sprintf(AgentArmTemplateStorePath, "v0.1.0"),
	Location:                "eastus",
	SignozAPIKey:            "test-api-key",
	SignozAPIUrl:            "https://signoz.example.com",
	SignozIngestionURL:      "https://ingest.example.com",
	SignozIngestionKey:      "test-ingest-key",
	AccountID:               "acct-123",
	AgentVersion:            "v0.1.0",
	StackName:               AgentDeploymentStackName,
	ParamLocation:           armParamLocation,
	ParamSignozAPIKey:       armParamSignozAPIKey,
	ParamSignozAPIUrl:       armParamSignozAPIUrl,
	ParamSignozIngestionURL: armParamSignozIngestionURL,
	ParamSignozIngestionKey: armParamSignozIngestionKey,
	ParamAccountID:          armParamAccountID,
	ParamAgentVersion:       armParamAgentVersion,
	ParamRgName:             armParamRgName,
	ParamContainerEnvName:   armParamContainerEnvName,
	ParamDeploymentEnv:      armParamDeploymentEnv,
	DefaultRgName:           armDefaultRgName,
	DefaultContainerEnvName: armDefaultContainerEnvName,
	DefaultDeploymentEnv:    armDefaultDeploymentEnv,
}

func TestNewAzureConnectionCLICommand(t *testing.T) {
	cmd, err := newAzureConnectionCLICommand(testTemplateData)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	mustContain := []string{
		"az stack sub create",
		fmt.Sprintf("--name %s", AgentDeploymentStackName),
		fmt.Sprintf("--location %s", testTemplateData.Location),
		fmt.Sprintf("--template-uri %s", testTemplateData.TemplateURL),
		fmt.Sprintf("%s='%s'", armParamLocation, testTemplateData.Location),
		fmt.Sprintf("%s='%s'", armParamSignozAPIKey, testTemplateData.SignozAPIKey),
		fmt.Sprintf("%s='%s'", armParamSignozAPIUrl, testTemplateData.SignozAPIUrl),
		fmt.Sprintf("%s='%s'", armParamSignozIngestionURL, testTemplateData.SignozIngestionURL),
		fmt.Sprintf("%s='%s'", armParamSignozIngestionKey, testTemplateData.SignozIngestionKey),
		fmt.Sprintf("%s='%s'", armParamAccountID, testTemplateData.AccountID),
		fmt.Sprintf("%s='%s'", armParamAgentVersion, testTemplateData.AgentVersion),
		"--action-on-unmanage deleteAll",
		"--deny-settings-mode denyDelete",
	}

	for _, fragment := range mustContain {
		if !strings.Contains(cmd, fragment) {
			t.Errorf("CLI command missing %q\ngot:\n%s", fragment, cmd)
		}
	}

	// Lines must be joined with the bash line-continuation separator.
	if !strings.Contains(cmd, " \\\n") {
		t.Errorf("CLI command missing line-continuation separator ' \\\\\\n'\ngot:\n%s", cmd)
	}
}

func TestNewAzureConnectionPowerShellCommand(t *testing.T) {
	cmd, err := newAzureConnectionPowerShellCommand(testTemplateData)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	mustContain := []string{
		"New-AzSubscriptionDeploymentStack",
		fmt.Sprintf("-Name \"%s\"", AgentDeploymentStackName),
		fmt.Sprintf("-Location \"%s\"", testTemplateData.Location),
		fmt.Sprintf("-TemplateUri \"%s\"", testTemplateData.TemplateURL),
		armParamLocation,
		armParamSignozAPIKey,
		armParamSignozAPIUrl,
		armParamSignozIngestionURL,
		armParamSignozIngestionKey,
		armParamAccountID,
		armParamAgentVersion,
		armParamRgName,
		armParamContainerEnvName,
		armParamDeploymentEnv,
		armDefaultRgName,
		armDefaultContainerEnvName,
		armDefaultDeploymentEnv,
		"-ActionOnUnmanage \"deleteAll\"",
		"-DenySettingsMode \"denyDelete\"",
	}

	for _, fragment := range mustContain {
		if !strings.Contains(cmd, fragment) {
			t.Errorf("PowerShell command missing %q\ngot:\n%s", fragment, cmd)
		}
	}

	// Final command must not end with a line-continuation backtick.
	if strings.HasSuffix(strings.TrimSpace(cmd), "`") {
		t.Errorf("PowerShell command must not end with a backtick\ngot:\n%s", cmd)
	}
}

func TestNewAzureConnectionArtifact(t *testing.T) {
	accountID := valuer.GenerateUUID()
	agentVersion := "v0.1.0"
	creds := &Credentials{
		SigNozAPIURL: "https://signoz.example.com",
		SigNozAPIKey: "test-api-key",
		IngestionURL: "https://ingest.example.com",
		IngestionKey: "test-ingest-key",
	}
	cfg := &AzurePostableAccountConfig{
		DeploymentRegion: "eastus",
		ResourceGroups:   []string{"rg1"},
	}

	artifact, err := NewAzureConnectionArtifact(accountID, agentVersion, creds, cfg)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	if artifact.CLICommand == "" {
		t.Error("CLICommand must not be empty")
	}
	if artifact.CloudPowerShellCommand == "" {
		t.Error("CloudPowerShellCommand must not be empty")
	}
	if !strings.Contains(artifact.CLICommand, accountID.StringValue()) {
		t.Errorf("CLICommand must contain accountID %q", accountID.StringValue())
	}
	if !strings.Contains(artifact.CloudPowerShellCommand, accountID.StringValue()) {
		t.Errorf("CloudPowerShellCommand must contain accountID %q", accountID.StringValue())
	}
}
