package usecase

import (
	"context"
	"fmt"
	"path"
	"strings"

	"source.toby3d.me/toby3d/home/internal/domain"
	"source.toby3d.me/toby3d/home/internal/resource"
)

type resourceUseCase struct {
	resources resource.Repository
}

func NewResourceUseCase(resources resource.Repository) resource.UseCase {
	return &resourceUseCase{
		resources: resources,
	}
}

func (ucase *resourceUseCase) Do(ctx context.Context, p string) (*domain.Resource, error) {
	r, err := ucase.resources.Get(ctx, strings.TrimPrefix(path.Clean(p), "/"))
	if err != nil {
		return nil, fmt.Errorf("cannot get resource file: %w", err)
	}

	return r, nil
}
