package host

import (
	"context"
	"strings"

	"hyapp/pkg/xerr"
)

const (
	CapabilityManagerCenter        = "manager_center"
	CapabilityManagerResourceGrant = "manager_resource_grant"
)

// CheckBusinessCapability 是 App/H5 业务入口的服务端身份边界。
// 首版经理中心只认 active Agency owner，不把客户端自报角色或普通 Agency 成员当作授权依据。
func (s *Service) CheckBusinessCapability(ctx context.Context, actorUserID int64, capability string) (bool, string, error) {
	if s.repository == nil {
		return false, "", xerr.New(xerr.Unavailable, "host repository is not configured")
	}
	if actorUserID <= 0 {
		return false, "", xerr.New(xerr.InvalidArgument, "actor_user_id is required")
	}
	capability = strings.ToLower(strings.TrimSpace(capability))
	switch capability {
	case CapabilityManagerCenter, CapabilityManagerResourceGrant:
		_, ok, err := s.repository.HasActiveAgencyOwner(ctx, actorUserID)
		if err != nil {
			return false, "", err
		}
		if !ok {
			return false, "active_agency_owner_required", nil
		}
		return true, "", nil
	default:
		return false, "", xerr.New(xerr.InvalidArgument, "business capability is invalid")
	}
}
