<?php

namespace Datalumo\PhpSdk\Resources;

use Datalumo\PhpSdk\DataObjects\ChatResponse;
use Datalumo\PhpSdk\DataObjects\Collection;
use Datalumo\PhpSdk\DataObjects\Entry;
use Datalumo\PhpSdk\DataObjects\PaginatedResponse;
use Datalumo\PhpSdk\DataObjects\SearchResult;
use Datalumo\PhpSdk\DataObjects\StreamResponse;
use Datalumo\PhpSdk\DataObjects\SummaryResponse;
use Datalumo\PhpSdk\HttpClient;

class CollectionResource
{
    public function __construct(private readonly HttpClient $client) {}

    public function list(?string $project = null, int $page = 1): PaginatedResponse
    {
        $query = array_filter(['project' => $project, 'page' => $page]);

        return PaginatedResponse::fromArray(
            $this->client->get('collections', $query),
            Collection::fromArray(...),
        );
    }

    public function create(string $name, ?string $project = null): Collection
    {
        return Collection::fromArray(
            $this->client->post('collections', array_filter([
                'name' => $name,
                'project' => $project,
            ]))['data'],
        );
    }

    public function update(string $collectionId, string $name, ?string $project = null): Collection
    {
        return Collection::fromArray(
            $this->client->patch("collections/{$collectionId}", array_filter([
                'name' => $name,
                'project' => $project,
            ]))['data'],
        );
    }

    public function delete(string $collectionId): void
    {
        $this->client->delete("collections/{$collectionId}");
    }

    /**
     * @param array{
     *     query: string,
     *     threshold?: float,
     *     meta?: array<string, string>,
     *     page?: int,
     * } $params
     */
    public function search(string $collectionId, array $params): PaginatedResponse
    {
        return PaginatedResponse::fromArray(
            $this->client->get("collections/{$collectionId}/search", $params),
            Entry::fromArray(...),
        );
    }

    /**
     * @param array{
     *     query: string,
     *     threshold?: float,
     *     format?: string,
     *     locale?: string,
     *     page?: int,
     * } $params
     */
    public function summarise(string $collectionId, array $params): SummaryResponse
    {
        $params['stream'] = false;

        return SummaryResponse::fromArray(
            $this->client->get("collections/{$collectionId}/summarise", $params),
        );
    }

    /**
     * @param array{
     *     message: string,
     *     conversation_id?: string,
     * } $params
     */
    public function chat(string $collectionId, array $params): ChatResponse
    {
        $params['stream'] = false;

        return ChatResponse::fromArray(
            $this->client->post("collections/{$collectionId}/chat", $params),
        );
    }

    /**
     * @param array{
     *     query: string,
     *     threshold?: float,
     *     format?: string,
     *     locale?: string,
     * } $params
     */
    public function streamSummarise(string $collectionId, array $params): StreamResponse
    {
        $params['stream'] = true;

        return $this->client->getStream("collections/{$collectionId}/summarise", $params);
    }

    /**
     * @param array{
     *     message: string,
     *     conversation_id?: string,
     * } $params
     */
    public function streamChat(string $collectionId, array $params): StreamResponse
    {
        $params['stream'] = true;

        return $this->client->postStream("collections/{$collectionId}/chat", $params);
    }
}
