package alicloud

import (
	"fmt"

	"github.com/PaesslerAG/jsonpath"
	"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
	"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceAlicloudResourceManagerControlPolicyAttachments() *schema.Resource {
	return &schema.Resource{
		Read: dataSourceAlicloudResourceManagerControlPolicyAttachmentsRead,
		Schema: map[string]*schema.Schema{
			"policy_type": {
				Type:     schema.TypeString,
				Optional: true,
				ForceNew: true,
			},
			"language": {
				Type:     schema.TypeString,
				Optional: true,
				ForceNew: true,
			},
			"target_id": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"ids": {
				Type:     schema.TypeList,
				Elem:     &schema.Schema{Type: schema.TypeString},
				Computed: true,
			},
			"output_file": {
				Type:     schema.TypeString,
				Optional: true,
			},
			"attachments": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"id": {
							Type:     schema.TypeString,
							Computed: true,
						},
						"attach_date": {
							Type:     schema.TypeString,
							Computed: true,
						},
						"description": {
							Type:     schema.TypeString,
							Computed: true,
						},
						"policy_id": {
							Type:     schema.TypeString,
							Computed: true,
						},
						"policy_name": {
							Type:     schema.TypeString,
							Computed: true,
						},
						"policy_type": {
							Type:     schema.TypeString,
							Computed: true,
						},
					},
				},
			},
		},
	}
}

func dataSourceAlicloudResourceManagerControlPolicyAttachmentsRead(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)

	action := "ListControlPolicyAttachmentsForTarget"
	request := make(map[string]interface{})
	if v, ok := d.GetOk("language"); ok {
		request["Language"] = v
	}
	request["TargetId"] = d.Get("target_id")
	var objects []map[string]interface{}
	var response map[string]interface{}
	var err error
	response, err = client.RpcPost("ResourceManager", "2020-03-31", action, nil, request, true)
	if err != nil {
		return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_resource_manager_control_policy_attachments", action, AlibabaCloudSdkGoERROR)
	}
	addDebug(action, response, request)

	resp, err := jsonpath.Get("$.ControlPolicyAttachments.ControlPolicyAttachment", response)
	if err != nil {
		return WrapErrorf(err, FailedGetAttributeMsg, action, "$.ControlPolicyAttachments.ControlPolicyAttachment", response)
	}
	result, _ := resp.([]interface{})
	for _, v := range result {
		item := v.(map[string]interface{})
		if policyType, ok := d.GetOk("policy_type"); ok && policyType.(string) != fmt.Sprint(item["PolicyType"]) {
			continue
		}
		objects = append(objects, item)
	}
	ids := make([]string, 0)
	s := make([]map[string]interface{}, 0)
	for _, object := range objects {
		mapping := map[string]interface{}{
			"id":          fmt.Sprint(object["PolicyId"], ":", request["TargetId"]),
			"attach_date": object["AttachDate"],
			"description": object["Description"],
			"policy_id":   object["PolicyId"],
			"policy_name": object["PolicyName"],
			"policy_type": object["PolicyType"],
		}
		ids = append(ids, fmt.Sprint(object["PolicyId"], ":", request["TargetId"]))
		s = append(s, mapping)
	}

	d.SetId(dataResourceIdHash(ids))
	if err := d.Set("ids", ids); err != nil {
		return WrapError(err)
	}

	if err := d.Set("attachments", s); err != nil {
		return WrapError(err)
	}
	if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
		writeToFile(output.(string), s)
	}

	return nil
}
