package adminauthorizationservice

import (
	"context"

	"git.gocasts.ir/ebhomengo/niki/entity"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

type Repository interface {
	GetAdminPermissions(ctx context.Context, adminID uint, role entity.AdminRole) ([]entity.AdminPermission, error)
}

type Service struct {
	repo Repository
}

func New(repo Repository) Service {
	return Service{repo: repo}
}

func (s Service) CheckAccess(ctx context.Context, adminID uint, role entity.AdminRole, permissions ...entity.AdminPermission) (bool, error) {
	const op = "adminauthorizationservice.CheckAccess"

	AdminPermissions, err := s.repo.GetAdminPermissions(ctx, adminID, role)
	if err != nil {
		return false, richerror.New(op).WithErr(err)
	}

	for _, p := range permissions {
		for _, ap := range AdminPermissions {
			if p == ap {
				return true, nil
			}
		}
	}

	return false, nil
}
