<?php

namespace App\Tests\Entity;

use App\Entity\Attestation;
use PHPUnit\Framework\TestCase;

class AttestationTest extends TestCase
{
    public function testConstructor(): void
    {
        $a = new Attestation('access', '127.0.0.1', 'test@test.com', 'secret');
        $this->assertNull($a->getId());
        $this->assertSame('access', $a->getType());
        $this->assertSame('127.0.0.1', $a->getIp());
        $this->assertSame('test@test.com', $a->getEmail());
        $this->assertNotEmpty($a->getReference());
        $this->assertNotEmpty($a->getHmac());
        $this->assertSame('pending', $a->getStatus());
        $this->assertInstanceOf(\DateTimeImmutable::class, $a->getCreatedAt());
    }

    public function testReferencePrefix(): void
    {
        $access = new Attestation('access', '1.1.1.1', 'a@t.com', 's');
        $this->assertStringContainsString('ACC', $access->getReference());

        $deletion = new Attestation('deletion', '1.1.1.1', 'a@t.com', 's');
        $this->assertStringContainsString('DEL', $deletion->getReference());

        $noData = new Attestation('no_data', '1.1.1.1', 'a@t.com', 's');
        $this->assertStringContainsString('ABS', $noData->getReference());
    }

    public function testPdfFiles(): void
    {
        $a = new Attestation('access', '1.1.1.1', 'a@t.com', 's');
        $this->assertNull($a->getPdfFileUnsigned());
        $this->assertNull($a->getPdfFileSigned());
        $this->assertNull($a->getPdfFileCertificate());

        $a->setPdfFileUnsigned('/path/unsigned.pdf');
        $a->setPdfFileSigned('/path/signed.pdf');
        $a->setPdfFileCertificate('/path/cert.pdf');

        $this->assertSame('/path/unsigned.pdf', $a->getPdfFileUnsigned());
        $this->assertSame('/path/signed.pdf', $a->getPdfFileSigned());
        $this->assertSame('/path/cert.pdf', $a->getPdfFileCertificate());
    }

    public function testSubmitterId(): void
    {
        $a = new Attestation('access', '1.1.1.1', 'a@t.com', 's');
        $this->assertNull($a->getSubmitterId());

        $a->setSubmitterId(42);
        $this->assertSame(42, $a->getSubmitterId());
    }

    public function testMarkAsSigned(): void
    {
        $a = new Attestation('access', '1.1.1.1', 'a@t.com', 's');
        $a->markAsSigned();
        $this->assertSame('signed', $a->getStatus());
    }

    public function testMarkAsSent(): void
    {
        $a = new Attestation('access', '1.1.1.1', 'a@t.com', 's');
        $a->markAsSent();
        $this->assertSame('sent', $a->getStatus());
    }

    public function testVerifyValid(): void
    {
        $a = new Attestation('access', '1.1.1.1', 'a@t.com', 'my-secret');
        $this->assertTrue($a->verify('my-secret'));
    }

    public function testVerifyInvalid(): void
    {
        $a = new Attestation('access', '1.1.1.1', 'a@t.com', 'my-secret');
        $this->assertFalse($a->verify('wrong-secret'));
    }

    public function testEmailTracking(): void
    {
        $a = new Attestation('access', '1.1.1.1', 'a@t.com', 's');
        $this->assertNull($a->getEmailTracking());

        $tracking = new \App\Entity\EmailTracking('msg-1', 'a@t.com', 'Subject');
        $result = $a->setEmailTracking($tracking);
        $this->assertSame($tracking, $a->getEmailTracking());
        $this->assertSame($a, $result);

        $a->setEmailTracking(null);
        $this->assertNull($a->getEmailTracking());
    }
}
