package accessgroup

import (
	"context"

	"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
)

func dataSourceAccessGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
	accessGroup, err := utilityAccessGroupCheckPresence(ctx, d, m)
	if err != nil {
		d.SetId("")
		return diag.FromErr(err)
	}

	flattenAccessGroupDataSource(d, accessGroup)
	d.SetId(accessGroup.ID)
	return nil
}

func dataSourceAccessGroupSchemaMake() map[string]*schema.Schema {
	res := map[string]*schema.Schema{
		"access_group_id": {
			Type:        schema.TypeString,
			Required:    true,
			Description: "The unique access group ID",
		},
		"display_name": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "Display name",
		},
		"comment": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "Comment description",
		},
		"created_at": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "Creation timestamp",
		},
		"net_object_access_group": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "Net object access group configuration",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"access_group_id": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Access group ID",
					},
					"id": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Unique identifier",
					},
					"version_id": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "Version identifier",
					},
				},
			},
		},
	}

	return res
}

func DataSourceAccessGroup() *schema.Resource {
	return &schema.Resource{
		SchemaVersion: 1,

		ReadContext: dataSourceAccessGroupRead,

		Timeouts: &schema.ResourceTimeout{
			Read:    &constants.Timeout30s,
			Default: &constants.Timeout60s,
		},

		Schema: dataSourceAccessGroupSchemaMake(),
	}
}
