package adminrole

import "errors"

var ErrDependencyUnavailable = errors.New("dependency unavailable")

type Service struct {
	repo *Repository
}

func NewService(repo *Repository) *Service {
	return &Service{repo: repo}
}

func (s *Service) List() ([]RoleDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	return s.repo.List()
}

func (s *Service) FindByID(id uint64) (*RoleDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	return s.repo.FindByID(id)
}

func (s *Service) Create(req CreateRoleRequest) (*RoleDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	if req.Code == "" || req.Name == "" {
		return nil, errors.New("code and name are required")
	}
	return s.repo.Create(req)
}

func (s *Service) Update(id uint64, req UpdateRoleRequest) (*RoleDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	return s.repo.Update(id, req)
}

func (s *Service) Delete(id uint64) error {
	if s.repo == nil {
		return ErrDependencyUnavailable
	}
	return s.repo.Delete(id)
}

func (s *Service) AssignPermissions(roleID uint64, req AssignPermissionsRequest) error {
	if s.repo == nil {
		return ErrDependencyUnavailable
	}
	return s.repo.AssignPermissions(roleID, req.PermissionIDs)
}

func (s *Service) ListPermissions() ([]PermissionDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	return s.repo.ListPermissions()
}
