package aws

import (
	"testing"

	ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
	"github.com/stretchr/testify/assert"
)

func TestIsVPCEndpointReusable(t *testing.T) {
	svc := "com.amazonaws.us-east-1.s3"
	vpc := "vpc-123456"
	et := "Interface"

	tests := []struct {
		name         string
		mockEndpoint func() *ec2types.VpcEndpoint
		want         bool
	}{
		{
			name: "ready and matching",
			mockEndpoint: func() *ec2types.VpcEndpoint {
				return &ec2types.VpcEndpoint{
					ServiceName:     &svc,
					VpcId:           &vpc,
					VpcEndpointType: ec2types.VpcEndpointTypeInterface,
					State:           ec2types.StateAvailable,
				}
			},
			want: true,
		},
		{
			name: "deleted state",
			mockEndpoint: func() *ec2types.VpcEndpoint {
				return &ec2types.VpcEndpoint{
					ServiceName:     &svc,
					VpcId:           &vpc,
					VpcEndpointType: ec2types.VpcEndpointTypeInterface,
					State:           ec2types.StateDeleted,
				}
			},
			want: false,
		},
		{
			name: "deleting state",
			mockEndpoint: func() *ec2types.VpcEndpoint {
				return &ec2types.VpcEndpoint{
					ServiceName:     &svc,
					VpcId:           &vpc,
					VpcEndpointType: ec2types.VpcEndpointTypeInterface,
					State:           ec2types.StateDeleting,
				}
			},
			want: false,
		},
		{
			name: "mismatch type",
			mockEndpoint: func() *ec2types.VpcEndpoint {
				return &ec2types.VpcEndpoint{
					ServiceName:     &svc,
					VpcId:           &vpc,
					VpcEndpointType: ec2types.VpcEndpointTypeGateway,
					State:           ec2types.StateAvailable,
				}
			},
			want: false,
		},
		{
			name: "mismatch service",
			mockEndpoint: func() *ec2types.VpcEndpoint {
				wrongSvc := "com.amazonaws.us-east-1.dynamodb"
				return &ec2types.VpcEndpoint{
					ServiceName:     &wrongSvc,
					VpcId:           &vpc,
					VpcEndpointType: ec2types.VpcEndpointTypeInterface,
					State:           ec2types.StateAvailable,
				}
			},
			want: false,
		},
		{
			name: "mismatch vpc",
			mockEndpoint: func() *ec2types.VpcEndpoint {
				wrongVpc := "vpc-789012"
				return &ec2types.VpcEndpoint{
					ServiceName:     &svc,
					VpcId:           &wrongVpc,
					VpcEndpointType: ec2types.VpcEndpointTypeInterface,
					State:           ec2types.StateAvailable,
				}
			},
			want: false,
		},
		{
			name: "nil service name",
			mockEndpoint: func() *ec2types.VpcEndpoint {
				return &ec2types.VpcEndpoint{
					ServiceName:     nil,
					VpcId:           &vpc,
					VpcEndpointType: ec2types.VpcEndpointTypeInterface,
					State:           ec2types.StateAvailable,
				}
			},
			want: false,
		},
		{
			name: "nil vpc id",
			mockEndpoint: func() *ec2types.VpcEndpoint {
				return &ec2types.VpcEndpoint{
					ServiceName:     &svc,
					VpcId:           nil,
					VpcEndpointType: ec2types.VpcEndpointTypeInterface,
					State:           ec2types.StateAvailable,
				}
			},
			want: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equal(t, tt.want, isVPCEndpointReusable(tt.mockEndpoint(), et, svc, vpc))
		})
	}
}
