package permission

import (
	"context"

	"gateway/internal/model/permission/domain/enum"
	"gateway/internal/svc"
	"gateway/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteRoleMappingLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewDeleteRoleMappingLogic returns the delete-mapping logic.
func NewDeleteRoleMappingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteRoleMappingLogic {
	return &DeleteRoleMappingLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// DeleteRoleMapping removes an external→internal mapping by external key.
func (l *DeleteRoleMappingLogic) DeleteRoleMapping(req *types.DeleteRoleMappingReq) error {
	if l.svcCtx.PermissionRoleMapping == nil {
		return errb.SysNotImplemented("permission module not configured")
	}
	actor, err := ActorFromContext(l.ctx)
	if err != nil {
		return errb.AuthUnauthorized(err.Error()).WithCause(err)
	}
	return l.svcCtx.PermissionRoleMapping.Delete(
		l.ctx,
		actor.TenantID,
		enum.RoleSource(req.ExternalSource),
		req.ExternalKey,
	)
}
