package services

import (
	"context"
	"git.sch9.ru/new_gate/ms-tester/internal/lib"
	"git.sch9.ru/new_gate/ms-tester/internal/models"
)

type TaskStorage interface {
	CreateTask(ctx context.Context, task models.Task) (int32, error)
	DeleteTask(ctx context.Context, id int32) error
}

type TaskService struct {
	taskStorage       TaskStorage
	permissionService IPermissionService
}

func NewTaskService(
	taskStorage TaskStorage,
	permissionService IPermissionService,
) *TaskService {
	return &TaskService{
		taskStorage:       taskStorage,
		permissionService: permissionService,
	}
}

func (service *TaskService) CreateTask(ctx context.Context, task models.Task) (int32, error) {
	if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
		return 0, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
	}
	return service.taskStorage.CreateTask(ctx, task)
}

func (service *TaskService) DeleteTask(ctx context.Context, id int32) error {
	if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
		return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
	}
	return service.taskStorage.DeleteTask(ctx, id)
}
