<?php

use Datalumo\PhpSdk\DataObjects\ChatResponse;
use Datalumo\PhpSdk\DataObjects\Entry;
use Datalumo\PhpSdk\DataObjects\Integration;
use Datalumo\PhpSdk\DataObjects\PaginatedResponse;
use Datalumo\PhpSdk\DataObjects\SearchResult;
use Datalumo\PhpSdk\DataObjects\SummaryResponse;
use Datalumo\PhpSdk\HttpClient;
use Datalumo\PhpSdk\Resources\IntegrationResource;

beforeEach(function () {
    $this->http = Mockery::mock(HttpClient::class);
    $this->resource = new IntegrationResource($this->http);
});

$integrationData = [
    'id' => 'int-1',
    'name' => 'My Chatbot',
    'type' => 'chatbot',
    'accent_color' => '#000',
    'allowed_domains' => [],
    'is_active' => true,
    'settings' => [],
    'collection_ids' => ['col-1'],
    'created_at' => '2026-01-01T00:00:00Z',
    'updated_at' => '2026-01-01T00:00:00Z',
];

it('lists integrations', function () use ($integrationData) {
    $this->http->shouldReceive('get')
        ->with('integrations', ['page' => 1])
        ->once()
        ->andReturn([
            'data' => [$integrationData],
            'meta' => ['current_page' => 1, 'last_page' => 1, 'per_page' => 15, 'total' => 1],
        ]);

    $result = $this->resource->list();

    expect($result)->toBeInstanceOf(PaginatedResponse::class)
        ->and($result->data[0])->toBeInstanceOf(Integration::class);
});

it('lists integrations filtered by type and project', function () {
    $this->http->shouldReceive('get')
        ->with('integrations', ['type' => 'chatbot', 'project' => 'docs', 'page' => 1])
        ->once()
        ->andReturn([
            'data' => [],
            'meta' => ['current_page' => 1, 'last_page' => 1, 'per_page' => 15, 'total' => 0],
        ]);

    $this->resource->list(type: 'chatbot', project: 'docs');
});

it('gets a single integration', function () use ($integrationData) {
    $this->http->shouldReceive('get')
        ->with('integrations/int-1')
        ->once()
        ->andReturn(['data' => $integrationData]);

    $result = $this->resource->get('int-1');

    expect($result)->toBeInstanceOf(Integration::class)
        ->and($result->name)->toBe('My Chatbot');
});

it('creates an integration', function () use ($integrationData) {
    $createData = ['type' => 'chatbot', 'name' => 'My Chatbot', 'collection_ids' => ['col-1']];

    $this->http->shouldReceive('post')
        ->with('integrations', $createData)
        ->once()
        ->andReturn(['data' => $integrationData]);

    $result = $this->resource->create($createData);

    expect($result)->toBeInstanceOf(Integration::class);
});

it('updates an integration', function () use ($integrationData) {
    $this->http->shouldReceive('patch')
        ->with('integrations/int-1', ['name' => 'Updated'])
        ->once()
        ->andReturn(['data' => array_merge($integrationData, ['name' => 'Updated'])]);

    $result = $this->resource->update('int-1', ['name' => 'Updated']);

    expect($result->name)->toBe('Updated');
});

it('deletes an integration', function () {
    $this->http->shouldReceive('delete')
        ->with('integrations/int-1')
        ->once();

    $this->resource->delete('int-1');
});

it('searches an integration', function () {
    $this->http->shouldReceive('get')
        ->with('integrations/int-1/search', ['query' => 'test'])
        ->once()
        ->andReturn([
            'data' => [
                [
                    'id' => 'entry-1',
                    'collection_id' => 'col-1',
                    'raw_text' => 'Content',

                    'created_at' => '2026-01-01T00:00:00Z',
                    'updated_at' => '2026-01-01T00:00:00Z',
                ],
            ],
            'meta' => ['current_page' => 1, 'last_page' => 1, 'per_page' => 15, 'total' => 1],
            'summarisable' => true,
        ]);

    $result = $this->resource->search('int-1', ['query' => 'test']);

    expect($result)->toBeInstanceOf(SearchResult::class)
        ->and($result->summarisable)->toBeTrue()
        ->and($result->data[0])->toBeInstanceOf(Entry::class);
});

it('summarises an integration', function () {
    $this->http->shouldReceive('get')
        ->with('integrations/int-1/summarise', ['query' => 'test', 'stream' => false])
        ->once()
        ->andReturn([
            'summary' => 'A summary',
            'references' => [],
            'data' => [],
            'has_relevant_results' => true,
        ]);

    $result = $this->resource->summarise('int-1', ['query' => 'test']);

    expect($result)->toBeInstanceOf(SummaryResponse::class);
});

it('chats with an integration', function () {
    $this->http->shouldReceive('post')
        ->with('integrations/int-1/chat', ['message' => 'hello', 'stream' => false])
        ->once()
        ->andReturn(['conversation_id' => 'conv-1', 'message' => 'Hi!']);

    $result = $this->resource->chat('int-1', ['message' => 'hello']);

    expect($result)->toBeInstanceOf(ChatResponse::class);
});

it('records an event', function () {
    $this->http->shouldReceive('post')
        ->with('integrations/int-1/events', ['event_type' => 'click', 'meta' => ['url' => 'https://example.com']])
        ->once()
        ->andReturn(['ok' => true]);

    $this->resource->recordEvent('int-1', [
        'event_type' => 'click',
        'meta' => ['url' => 'https://example.com'],
    ]);
});
