package lightsail

import (
	"context"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/service/lightsail"
	"github.com/cloudquery/cq-provider-aws/client"
	"github.com/cloudquery/cq-provider-sdk/provider/diag"
	"github.com/cloudquery/cq-provider-sdk/provider/schema"
)

//go:generate cq-gen --resource static_ips --config gen.hcl --output .
func StaticIps() *schema.Table {
	return &schema.Table{
		Name:         "aws_lightsail_static_ips",
		Description:  "Describes a static IP",
		Resolver:     fetchLightsailStaticIps,
		Multiplex:    client.ServiceAccountRegionMultiplexer("lightsail"),
		IgnoreError:  client.IgnoreAccessDeniedServiceDisabled,
		DeleteFilter: client.DeleteAccountRegionFilter,
		Options:      schema.TableCreationOptions{PrimaryKeys: []string{"arn"}},
		Columns: []schema.Column{
			{
				Name:        "account_id",
				Description: "The AWS Account ID of the resource.",
				Type:        schema.TypeString,
				Resolver:    client.ResolveAWSAccount,
			},
			{
				Name:        "region",
				Description: "The AWS Region of the resource.",
				Type:        schema.TypeString,
				Resolver:    client.ResolveAWSRegion,
			},
			{
				Name:        "arn",
				Description: "The Amazon Resource Name (ARN) of the static IP (eg, arn:aws:lightsail:us-east-2:123456789101:StaticIp/9cbb4a9e-f8e3-4dfe-b57e-12345EXAMPLE)",
				Type:        schema.TypeString,
			},
			{
				Name:        "attached_to",
				Description: "The instance where the static IP is attached (eg, Amazon_Linux-1GB-Ohio-1)",
				Type:        schema.TypeString,
			},
			{
				Name:        "created_at",
				Description: "The timestamp when the static IP was created (eg, 1479735304222)",
				Type:        schema.TypeTimestamp,
			},
			{
				Name:        "ip_address",
				Description: "The static IP address",
				Type:        schema.TypeString,
			},
			{
				Name:        "is_attached",
				Description: "A Boolean value indicating whether the static IP is attached",
				Type:        schema.TypeBool,
			},
			{
				Name:        "availability_zone",
				Description: "The Availability Zone",
				Type:        schema.TypeString,
				Resolver:    schema.PathResolver("Location.AvailabilityZone"),
			},
			{
				Name:        "name",
				Description: "The name of the static IP (eg, StaticIP-Ohio-EXAMPLE)",
				Type:        schema.TypeString,
			},
			{
				Name:        "resource_type",
				Description: "The resource type (usually StaticIp)",
				Type:        schema.TypeString,
			},
			{
				Name:        "support_code",
				Description: "The support code",
				Type:        schema.TypeString,
			},
		},
	}
}

// ====================================================================================================================
//                                               Table Resolver Functions
// ====================================================================================================================

func fetchLightsailStaticIps(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
	var input lightsail.GetStaticIpsInput
	c := meta.(*client.Client)
	svc := c.Services().Lightsail
	for {
		response, err := svc.GetStaticIps(ctx, &input)
		if err != nil {
			return diag.WrapError(err)
		}
		res <- response.StaticIps
		if aws.ToString(response.NextPageToken) == "" {
			break
		}
		input.PageToken = response.NextPageToken
	}
	return nil
}
