package account_role

import (
	"strings"

	accountrolev1 "github.com/tech/sendico/pkg/proto/common/account_role/v1"
)

type AccountRole string

const (
	AccountRoleOperating  AccountRole = "operating"
	AccountRoleHold       AccountRole = "hold"
	AccountRoleTransit    AccountRole = "transit"
	AccountRoleSettlement AccountRole = "settlement"
	AccountRoleClearing   AccountRole = "clearing"
	AccountRolePending    AccountRole = "pending"
	AccountRoleReserve    AccountRole = "reserve"
	AccountRoleLiquidity  AccountRole = "liquidity"
	AccountRoleFee        AccountRole = "fee"
	AccountRoleChargeback AccountRole = "chargeback"
	AccountRoleAdjustment AccountRole = "adjustment"
)

const (
	MetadataKeyFromRole = "from_role"
	MetadataKeyToRole   = "to_role"
)

func Parse(value string) (AccountRole, bool) {
	switch strings.ToUpper(strings.TrimSpace(value)) {
	case "ACCOUNT_ROLE_OPERATING", "OPERATING":
		return AccountRoleOperating, true
	case "ACCOUNT_ROLE_HOLD", "HOLD":
		return AccountRoleHold, true
	case "ACCOUNT_ROLE_TRANSIT", "TRANSIT":
		return AccountRoleTransit, true
	case "ACCOUNT_ROLE_SETTLEMENT", "SETTLEMENT":
		return AccountRoleSettlement, true
	case "ACCOUNT_ROLE_CLEARING", "CLEARING":
		return AccountRoleClearing, true
	case "ACCOUNT_ROLE_PENDING", "PENDING":
		return AccountRolePending, true
	case "ACCOUNT_ROLE_RESERVE", "RESERVE":
		return AccountRoleReserve, true
	case "ACCOUNT_ROLE_LIQUIDITY", "LIQUIDITY":
		return AccountRoleLiquidity, true
	case "ACCOUNT_ROLE_FEE", "FEE":
		return AccountRoleFee, true
	case "ACCOUNT_ROLE_CHARGEBACK", "CHARGEBACK":
		return AccountRoleChargeback, true
	case "ACCOUNT_ROLE_ADJUSTMENT", "ADJUSTMENT":
		return AccountRoleAdjustment, true
	case "ACCOUNT_ROLE_UNSPECIFIED", "UNSPECIFIED", "":
		return "", true
	default:
		return "", false
	}
}

func ToProto(role AccountRole) accountrolev1.AccountRole {
	switch role {
	case AccountRoleOperating:
		return accountrolev1.AccountRole_OPERATING
	case AccountRoleHold:
		return accountrolev1.AccountRole_HOLD
	case AccountRoleTransit:
		return accountrolev1.AccountRole_TRANSIT
	case AccountRoleSettlement:
		return accountrolev1.AccountRole_SETTLEMENT
	case AccountRoleClearing:
		return accountrolev1.AccountRole_CLEARING
	case AccountRolePending:
		return accountrolev1.AccountRole_PENDING
	case AccountRoleReserve:
		return accountrolev1.AccountRole_RESERVE
	case AccountRoleLiquidity:
		return accountrolev1.AccountRole_LIQUIDITY
	case AccountRoleFee:
		return accountrolev1.AccountRole_FEE
	case AccountRoleChargeback:
		return accountrolev1.AccountRole_CHARGEBACK
	case AccountRoleAdjustment:
		return accountrolev1.AccountRole_ADJUSTMENT
	default:
		return accountrolev1.AccountRole_ACCOUNT_ROLE_UNSPECIFIED
	}
}

func FromProto(role accountrolev1.AccountRole) AccountRole {
	switch role {
	case accountrolev1.AccountRole_OPERATING:
		return AccountRoleOperating
	case accountrolev1.AccountRole_HOLD:
		return AccountRoleHold
	case accountrolev1.AccountRole_TRANSIT:
		return AccountRoleTransit
	case accountrolev1.AccountRole_SETTLEMENT:
		return AccountRoleSettlement
	case accountrolev1.AccountRole_CLEARING:
		return AccountRoleClearing
	case accountrolev1.AccountRole_PENDING:
		return AccountRolePending
	case accountrolev1.AccountRole_RESERVE:
		return AccountRoleReserve
	case accountrolev1.AccountRole_LIQUIDITY:
		return AccountRoleLiquidity
	case accountrolev1.AccountRole_FEE:
		return AccountRoleFee
	case accountrolev1.AccountRole_CHARGEBACK:
		return AccountRoleChargeback
	case accountrolev1.AccountRole_ADJUSTMENT:
		return AccountRoleAdjustment
	default:
		return ""
	}
}
