package account

import (
	"context"

	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
)

func (s *Service) GetRoleIDsForAccount(ctx context.Context, accountID uint) ([]uint, error) {
	account, err := s.accountStore.GetByID(ctx, accountID)
	if err != nil {
		return nil, errors.Wrap(errors.CodeInternalError, err, "查询账号失败")
	}

	if account.UserType == constants.UserTypeSuperAdmin {
		return []uint{}, nil
	}

	accountRoles, err := s.accountRoleStore.GetRoleIDsByAccountID(ctx, accountID)
	if err != nil {
		return nil, errors.Wrap(errors.CodeInternalError, err, "查询账号角色失败")
	}
	if len(accountRoles) > 0 {
		return accountRoles, nil
	}

	if account.UserType == constants.UserTypeAgent && account.ShopID != nil {
		shopRoles, err := s.shopRoleStore.GetRoleIDsByShopID(ctx, *account.ShopID)
		if err != nil {
			return nil, errors.Wrap(errors.CodeInternalError, err, "查询店铺角色失败")
		}
		return shopRoles, nil
	}

	return []uint{}, nil
}
