<?php

namespace App\Tests\Entity;

use App\Entity\Service;
use App\Entity\ServiceCategory;
use App\Entity\ServiceMessage;
use App\Entity\User;
use PHPUnit\Framework\TestCase;

class ServiceMessageTest extends TestCase
{
    private function createService(): Service
    {
        $category = new ServiceCategory('Web', 'web');

        return new Service('Test Service', 'test-service', $category);
    }

    public function testConstructorDefaults(): void
    {
        $service = $this->createService();
        $message = new ServiceMessage($service, 'Alert Title', 'Alert Content');

        $this->assertNull($message->getId());
        $this->assertSame($service, $message->getService());
        $this->assertSame('Alert Title', $message->getTitle());
        $this->assertSame('Alert Content', $message->getContent());
        $this->assertSame('info', $message->getSeverity());
        $this->assertTrue($message->isActive());
        $this->assertNull($message->getAuthor());
        $this->assertInstanceOf(\DateTimeImmutable::class, $message->getCreatedAt());
        $this->assertNull($message->getResolvedAt());
    }

    public function testConstructorWithSeverityAndAuthor(): void
    {
        $service = $this->createService();
        $user = new User();
        $user->setEmail('admin@test.com');
        $user->setFirstName('Admin');
        $user->setLastName('User');
        $user->setPassword('h');

        $message = new ServiceMessage($service, 'Critical', 'Server down', 'critical', $user);

        $this->assertSame('critical', $message->getSeverity());
        $this->assertSame($user, $message->getAuthor());
    }

    public function testResolve(): void
    {
        $service = $this->createService();
        $message = new ServiceMessage($service, 'Test', 'Content');

        $this->assertTrue($message->isActive());
        $this->assertNull($message->getResolvedAt());

        $result = $message->resolve();

        $this->assertSame($message, $result);
        $this->assertFalse($message->isActive());
        $this->assertInstanceOf(\DateTimeImmutable::class, $message->getResolvedAt());
    }
}
