package io.github.shirainbown.sharelink.download;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.index.query.QueryFactory;
import io.github.shirainbown.sharelink.model.DownloadResource;

/**
 * Shared lookup helpers for {@link DownloadResource}.
 */
@Component
@RequiredArgsConstructor
public class DownloadResourceService {

    private final ReactiveExtensionClient client;

    /**
     * Finds a resource by its (unique) slug.
     */
    public Mono<DownloadResource> findBySlug(String slug) {
        var options = ListOptions.builder()
            .fieldQuery(QueryFactory.equal("spec.slug", slug))
            .build();
        return client.listAll(DownloadResource.class, options, Sort.unsorted())
            .next();
    }
}
