<?php

namespace App\Tests\MessageHandler;

use App\Message\MeilisearchMessage;
use App\MessageHandler\MeilisearchMessageHandler;
use App\Service\MeilisearchService;
use PHPUnit\Framework\TestCase;

class MeilisearchMessageHandlerTest extends TestCase
{
    private MeilisearchService $meilisearch;
    private MeilisearchMessageHandler $handler;

    protected function setUp(): void
    {
        $this->meilisearch = $this->createMock(MeilisearchService::class);
        $this->handler = new MeilisearchMessageHandler($this->meilisearch);
    }

    public function testHandleCreateIndex(): void
    {
        $this->meilisearch->expects(self::once())
            ->method('request')
            ->with('POST', '/indexes', ['uid' => 'events', 'primaryKey' => 'uid']);

        ($this->handler)(new MeilisearchMessage('createIndex', 'events', ['primaryKey' => 'uid']));
    }

    public function testHandleDeleteIndex(): void
    {
        $this->meilisearch->expects(self::once())
            ->method('request')
            ->with('DELETE', '/indexes/events');

        ($this->handler)(new MeilisearchMessage('deleteIndex', 'events'));
    }

    public function testHandleAddDocuments(): void
    {
        $docs = [['id' => 1]];
        $this->meilisearch->expects(self::once())
            ->method('request')
            ->with('POST', '/indexes/events/documents', $docs);

        ($this->handler)(new MeilisearchMessage('addDocuments', 'events', ['documents' => $docs]));
    }

    public function testHandleUpdateDocuments(): void
    {
        $docs = [['id' => 1, 'title' => 'Updated']];
        $this->meilisearch->expects(self::once())
            ->method('request')
            ->with('PUT', '/indexes/events/documents', $docs);

        ($this->handler)(new MeilisearchMessage('updateDocuments', 'events', ['documents' => $docs]));
    }

    public function testHandleDeleteDocument(): void
    {
        $this->meilisearch->expects(self::once())
            ->method('request')
            ->with('DELETE', '/indexes/events/documents/42');

        ($this->handler)(new MeilisearchMessage('deleteDocument', 'events', ['documentId' => 42]));
    }

    public function testHandleDeleteDocuments(): void
    {
        $this->meilisearch->expects(self::once())
            ->method('request')
            ->with('POST', '/indexes/events/documents/delete-batch', [1, 2, 3]);

        ($this->handler)(new MeilisearchMessage('deleteDocuments', 'events', ['ids' => [1, 2, 3]]));
    }

    public function testHandleUpdateSettings(): void
    {
        $settings = ['searchableAttributes' => ['title']];
        $this->meilisearch->expects(self::once())
            ->method('request')
            ->with('PATCH', '/indexes/events/settings', $settings);

        ($this->handler)(new MeilisearchMessage('updateSettings', 'events', ['settings' => $settings]));
    }

    public function testUnknownActionThrowsException(): void
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('Unknown action: invalid');

        ($this->handler)(new MeilisearchMessage('invalid', 'events'));
    }
}
