<?php

declare(strict_types=1);

namespace App\Tests\Mcp\Tool\Comment;

use App\Entity\Comment;
use App\Tests\AbstractApiTestCase;

/**
 * @internal
 */
class CommentsToolTest extends AbstractApiTestCase
{
    public function testListComments(): void
    {
        $entityId = 'entity-'.uniqid();
        $this->createComment('First comment', 'machine', $entityId);
        $this->createComment('Second comment', 'machine', $entityId);
        $this->createComment('Other entity', 'machine', 'other-id');

        $session = $this->createMcpClient('ROLE_VIEWER');

        $data = $this->callMcpTool($session, 'list_comments', [
            'entityType' => 'machine',
            'entityId'   => $entityId,
        ]);

        $this->assertArrayHasKey('_parsed', $data);
        $this->assertSame(2, $data['_parsed']['total']);
        $this->assertCount(2, $data['_parsed']['items']);
    }

    public function testCreateComment(): void
    {
        $session = $this->createMcpClient('ROLE_VIEWER');

        $data = $this->callMcpTool($session, 'create_comment', [
            'content'    => 'A new comment',
            'entityType' => 'machine',
            'entityId'   => 'some-machine-id',
            'entityName' => 'Machine Alpha',
        ]);

        $this->assertArrayHasKey('_parsed', $data);
        $this->assertNotEmpty($data['_parsed']['id']);
        $this->assertSame('A new comment', $data['_parsed']['content']);
        $this->assertSame('machine', $data['_parsed']['entityType']);
        $this->assertSame('open', $data['_parsed']['status']);
        $this->assertSame('Machine Alpha', $data['_parsed']['entityName']);
    }

    public function testResolveComment(): void
    {
        $comment = $this->createComment('To resolve', 'piece', 'piece-123');
        $session = $this->createMcpClient('ROLE_GESTIONNAIRE');

        $data = $this->callMcpTool($session, 'resolve_comment', [
            'commentId' => $comment->getId(),
        ]);

        $this->assertArrayHasKey('_parsed', $data);
        $this->assertSame('resolved', $data['_parsed']['status']);
        $this->assertNotEmpty($data['_parsed']['resolvedByName']);
    }

    public function testUnresolvedCount(): void
    {
        $entityId = 'entity-'.uniqid();
        $this->createComment('Open 1', 'machine', $entityId, 'open');
        $this->createComment('Open 2', 'machine', $entityId, 'open');
        $this->createComment('Resolved', 'machine', $entityId, 'resolved');

        $session = $this->createMcpClient('ROLE_VIEWER');

        $data = $this->callMcpTool($session, 'get_unresolved_comments_count');

        $this->assertArrayHasKey('_parsed', $data);
        $this->assertGreaterThanOrEqual(2, $data['_parsed']['count']);
    }

    private function createComment(string $content, string $entityType, string $entityId, string $status = 'open'): Comment
    {
        $comment = new Comment();
        $comment->setContent($content);
        $comment->setEntityType($entityType);
        $comment->setEntityId($entityId);
        $comment->setAuthorId('test-author-id');
        $comment->setAuthorName('Test Author');
        $comment->setStatus($status);

        $em = $this->getEntityManager();
        $em->persist($comment);
        $em->flush();

        return $comment;
    }
}
