package ec2

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/arn"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-provider-aws/internal/conns"
	tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
	"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func DataSourceTransitGatewayAttachment() *schema.Resource {
	return &schema.Resource{
		Read: dataSourceTransitGatewayAttachmentRead,

		Schema: map[string]*schema.Schema{
			"arn": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"filter": CustomFiltersSchema(),
			"resource_id": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"resource_owner_id": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"resource_type": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"state": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"tags": tftags.TagsSchemaComputed(),
			"transit_gateway_attachment_id": {
				Type:     schema.TypeString,
				Optional: true,
				Computed: true,
			},
			"transit_gateway_id": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"transit_gateway_owner_id": {
				Type:     schema.TypeString,
				Computed: true,
			},
		},
	}
}

func dataSourceTransitGatewayAttachmentRead(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*conns.AWSClient).EC2Conn()
	ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

	input := &ec2.DescribeTransitGatewayAttachmentsInput{}

	input.Filters = append(input.Filters, BuildCustomFilterList(
		d.Get("filter").(*schema.Set),
	)...)

	if len(input.Filters) == 0 {
		// Don't send an empty filters list; the EC2 API won't accept it.
		input.Filters = nil
	}

	if v, ok := d.GetOk("transit_gateway_attachment_id"); ok {
		input.TransitGatewayAttachmentIds = aws.StringSlice([]string{v.(string)})
	}

	transitGatewayAttachment, err := FindTransitGatewayAttachment(conn, input)

	if err != nil {
		return tfresource.SingularDataSourceFindError("EC2 Transit Gateway Attachment", err)
	}

	transitGatewayAttachmentID := aws.StringValue(transitGatewayAttachment.TransitGatewayAttachmentId)
	d.SetId(transitGatewayAttachmentID)

	resourceOwnerID := aws.StringValue(transitGatewayAttachment.ResourceOwnerId)
	arn := arn.ARN{
		Partition: meta.(*conns.AWSClient).Partition,
		Service:   ec2.ServiceName,
		Region:    meta.(*conns.AWSClient).Region,
		AccountID: resourceOwnerID,
		Resource:  fmt.Sprintf("transit-gateway-attachment/%s", d.Id()),
	}.String()
	d.Set("arn", arn)
	d.Set("resource_id", transitGatewayAttachment.ResourceId)
	d.Set("resource_owner_id", resourceOwnerID)
	d.Set("resource_type", transitGatewayAttachment.ResourceType)
	d.Set("state", transitGatewayAttachment.State)
	d.Set("transit_gateway_attachment_id", transitGatewayAttachmentID)
	d.Set("transit_gateway_id", transitGatewayAttachment.TransitGatewayId)
	d.Set("transit_gateway_owner_id", transitGatewayAttachment.TransitGatewayOwnerId)

	if err := d.Set("tags", KeyValueTags(transitGatewayAttachment.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
		return fmt.Errorf("setting tags: %w", err)
	}

	return nil
}
