/*
Copyright 2026 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package azuretasks

import (
	"k8s.io/kops/upup/pkg/fi"
	"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
	"k8s.io/kops/upup/pkg/fi/cloudup/terraformWriter"
)

type terraformAzureApplicationSecurityGroup struct {
	Name              *string                  `cty:"name"`
	Location          *string                  `cty:"location"`
	ResourceGroupName *terraformWriter.Literal `cty:"resource_group_name"`
	Tags              map[string]string        `cty:"tags"`
}

func (*ApplicationSecurityGroup) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *ApplicationSecurityGroup) error {
	tf := &terraformAzureApplicationSecurityGroup{
		Name:              e.Name,
		Location:          new(t.Cloud.Region()),
		ResourceGroupName: e.ResourceGroup.terraformName(),
		Tags:              stringMap(e.Tags),
	}
	return t.RenderResource("azurerm_application_security_group", fi.ValueOf(e.Name), tf)
}

func (asg *ApplicationSecurityGroup) terraformID() *terraformWriter.Literal {
	return terraformWriter.LiteralProperty("azurerm_application_security_group", fi.ValueOf(asg.Name), "id")
}

func applicationSecurityGroupIDs(groups []*ApplicationSecurityGroup) []*terraformWriter.Literal {
	var ids []*terraformWriter.Literal
	for _, group := range groups {
		ids = append(ids, group.terraformID())
	}
	return ids
}

func applicationSecurityGroupNameIDs(names []*string) []*terraformWriter.Literal {
	var ids []*terraformWriter.Literal
	for _, name := range names {
		ids = append(ids, (&ApplicationSecurityGroup{Name: name}).terraformID())
	}
	return ids
}
