<?php

use Datalumo\PhpSdk\DataObjects\Entry;
use Datalumo\PhpSdk\DataObjects\PaginatedResponse;
use Datalumo\PhpSdk\HttpClient;
use Datalumo\PhpSdk\Resources\EntryResource;

beforeEach(function () {
    $this->http = Mockery::mock(HttpClient::class);
    $this->resource = new EntryResource($this->http, 'col-123');
});

it('lists entries', function () {
    $this->http->shouldReceive('get')
        ->with('collections/col-123/entries', ['page' => 1])
        ->once()
        ->andReturn([
            'data' => [
                [
                    'id' => 'entry-1',
                    'collection_id' => 'col-123',
                    'title' => 'Entry 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],
        ]);

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

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

it('creates an entry', function () {
    $data = ['raw_text' => 'Content', 'title' => 'My Entry'];

    $this->http->shouldReceive('post')
        ->with('collections/col-123/entries', $data)
        ->once()
        ->andReturn([
            'data' => [
                'id' => 'entry-new',
                'collection_id' => 'col-123',
                'title' => 'My Entry',
                'raw_text' => 'Content',
                'tags' => [],
                'created_at' => '2026-01-01T00:00:00Z',
                'updated_at' => '2026-01-01T00:00:00Z',
            ],
        ]);

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

    expect($result)->toBeInstanceOf(Entry::class)
        ->and($result->title)->toBe('My Entry');
});

it('upserts an entry', function () {
    $data = ['raw_text' => 'Content', 'source_type' => 'articles', 'source_id' => '42'];

    $this->http->shouldReceive('put')
        ->with('collections/col-123/entries/upsert', $data)
        ->once()
        ->andReturn([
            'data' => [
                'id' => 'entry-1',
                'collection_id' => 'col-123',
                'raw_text' => 'Content',
                'source_type' => 'articles',
                'source_id' => '42',
                'tags' => [],
                'created_at' => '2026-01-01T00:00:00Z',
                'updated_at' => '2026-01-01T00:00:00Z',
            ],
        ]);

    $result = $this->resource->upsert($data);

    expect($result)->toBeInstanceOf(Entry::class)
        ->and($result->sourceType)->toBe('articles');
});

it('batch upserts entries', function () {
    $entries = [
        ['raw_text' => 'Content 1', 'source_type' => 'articles', 'source_id' => '1'],
        ['raw_text' => 'Content 2', 'source_type' => 'articles', 'source_id' => '2'],
    ];

    $this->http->shouldReceive('post')
        ->with('collections/col-123/entries/batch', ['entries' => $entries])
        ->once()
        ->andReturn(['created' => 2, 'updated' => 0]);

    $result = $this->resource->batchUpsert($entries);

    expect($result)->toBe(['created' => 2, 'updated' => 0]);
});

it('updates an entry', function () {
    $this->http->shouldReceive('patch')
        ->with('collections/col-123/entries/entry-1', ['title' => 'Updated'])
        ->once()
        ->andReturn([
            'data' => [
                'id' => 'entry-1',
                'collection_id' => 'col-123',
                'title' => 'Updated',
                'raw_text' => 'Content',
                'tags' => [],
                'created_at' => '2026-01-01T00:00:00Z',
                'updated_at' => '2026-01-02T00:00:00Z',
            ],
        ]);

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

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

it('deletes an entry', function () {
    $this->http->shouldReceive('delete')
        ->with('collections/col-123/entries/entry-1')
        ->once();

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

it('deletes an entry by source', function () {
    $this->http->shouldReceive('delete')
        ->with('collections/col-123/entries/by-source/articles/42')
        ->once();

    $this->resource->deleteBySource('articles', '42');
});
