package imp

import (
	"errors"
	"fmt"
	"testing"

	l4g "github.com/alecthomas/log4go"
	"github.com/tidbcloud/qa-PlatformAPIAuto/common"
	"github.com/tidbcloud/qa-PlatformAPIAuto/models"
)

type Credential struct {
	// aws access key id
	AwsAccessKeyID string

	// aws role arn
	AwsRoleArn string

	// aws secret access key
	AwsSecretAccessKey string
}

type S3Import struct {
	*baseImport

	Credential *Credential
}

func NewS3Import(t *testing.T, dbaasInfo *common.DbassRequestInfo) *S3Import {
	return &S3Import{
		baseImport: &baseImport{
			DbassRequestInfo: dbaasInfo,
			T:                t,
		},
	}
}

func (i *S3Import) Create() {
	if i.Credential == nil {
		l4g.Error("credential is nil, can't create import")

		return
	}

	//i.Data.AwsRoleArn = i.Credential.AwsRoleArn
	// req.AwsAccessKeyID = i.Credential.AwsAccessKeyID
	// req.AwsSecretAccessKey = i.Credential.AwsSecretAccessKey

	i.baseImport.Create()
}

func (i *S3Import) CreateCredential(req *models.DataflowDataIngestionProfile) error {
	if i.ClusterId == "" {
		return errors.New("cluster id is empty, can't create credential")
	}

	i.Credential = &Credential{}
	i.Credential.AwsRoleArn = fmt.Sprintf("arn:aws:iam::676546228356:role/%s",
		i.createImportRole(
			*req.AwsAccountID,
			*req.AwsExternalID,
			i.CommonEnv.Ec2Host,
			i.CommonEnv.CerPath,
			i.CommonEnv.LoginUser,
		),
	)
	return nil
}

func importRoleName(clusterId string) string {
	return fmt.Sprintf("scene-test-import-cluster%s-role", clusterId)
}

func (i *S3Import) createImportRole(accountId, externalId, ec2Host, cerPath, loginUser string) string {
	// todo: maybe do not need policy.json
	cpcmd := "cp -f /home/ec2-user/policy.json /home/ec2-user/policy1.json"
	common.ConnectInstance(ec2Host, cerPath, loginUser, cpcmd)
	sedcmd := fmt.Sprintf("sed -i 's/AccountID/%s/g' /home/ec2-user/policy1.json", accountId)
	common.ConnectInstance(ec2Host, cerPath, loginUser, sedcmd)
	sedcmd = fmt.Sprintf("sed -i 's/ExternalIdData/%s/g' /home/ec2-user/policy1.json", externalId)
	common.ConnectInstance(ec2Host, cerPath, loginUser, sedcmd)

	importRole := importRoleName(i.ClusterId)

	s3cmd := fmt.Sprintf("aws iam create-role --role-name %s --assume-role-policy-document file:///home/ec2-user/policy1.json", importRole)
	common.ConnectInstance(ec2Host, cerPath, loginUser, s3cmd)
	s3cmd = fmt.Sprintf("aws iam attach-role-policy --role-name %s --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess", importRole)
	common.ConnectInstance(ec2Host, cerPath, loginUser, s3cmd)

	return importRole
}

func (i *S3Import) DeleteCredential() {
	i.deleteImportRole(
		i.CommonEnv.Ec2Host,
		i.CommonEnv.CerPath,
		i.CommonEnv.LoginUser,
	)
}

func (i *S3Import) deleteImportRole(ec2Host, cerPath, loginUser string) {
	importRole := importRoleName(i.ClusterId)

	s3cmd := fmt.Sprintf("aws iam detach-role-policy --role-name %s --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess", importRole)
	common.ConnectInstance(ec2Host, cerPath, loginUser, s3cmd)

	s3cmd = fmt.Sprintf("aws iam delete-role --role-name %s", importRoleName(i.ClusterId))
	common.ConnectInstance(ec2Host, cerPath, loginUser, s3cmd)
}
