package execution

import (
	"errors"
	"strings"
)

type RoleReadInputError struct {
	Message string
}

func (err *RoleReadInputError) Error() string {
	if err == nil || strings.TrimSpace(err.Message) == "" {
		return "Role read input is invalid"
	}
	return err.Message
}

type RoleReadDependencyError struct {
	cause error
}

func NewRoleReadDependencyError(cause error) error {
	if cause == nil {
		return nil
	}
	return &RoleReadDependencyError{cause: cause}
}

func (err *RoleReadDependencyError) Error() string {
	return "Role read dependency failed"
}

func (err *RoleReadDependencyError) Unwrap() error {
	if err == nil {
		return nil
	}
	return err.cause
}

func NewRoleReadInputError(message string) error {
	return &RoleReadInputError{Message: strings.TrimSpace(message)}
}

func IsRoleReadInputError(err error) bool {
	var input *RoleReadInputError
	return errors.As(err, &input)
}

func IsRoleReadDependencyError(err error) bool {
	var dependency *RoleReadDependencyError
	return errors.As(err, &dependency)
}

type RoleReadPrincipal struct {
	WorkspaceID string
	Subject     string
}

type RoleListInput struct {
	View   string
	Query  string
	Limit  int
	Cursor string
}

type RoleSummary struct {
	RoleID        string `json:"roleId"`
	Name          string `json:"name"`
	Description   string `json:"description,omitempty"`
	Visibility    string `json:"visibility"`
	RoleVersionID string `json:"roleVersionId"`
	CanManage     bool   `json:"canManage"`
}

type RoleDetail struct {
	RoleID        string `json:"roleId"`
	Name          string `json:"name"`
	Description   string `json:"description,omitempty"`
	Definition    string `json:"definition"`
	Visibility    string `json:"visibility"`
	RoleVersionID string `json:"roleVersionId"`
	CanManage     bool   `json:"canManage"`
}

type RoleListResult struct {
	Roles      []RoleSummary `json:"roles"`
	NextCursor string        `json:"nextCursor,omitempty"`
}
