<?php

namespace MintyPHP\Tests\Service\Import;

use MintyPHP\Http\SessionStore;
use MintyPHP\Service\Import\ImportStateStoreService;
use MintyPHP\Service\Import\ImportTempFileService;
use PHPUnit\Framework\TestCase;

class ImportStateStoreServiceTest extends TestCase
{
    protected function setUp(): void
    {
        $_SESSION = [];
    }

    public function testSetGetAndClearState(): void
    {
        $tempFileService = $this->createMock(ImportTempFileService::class);
        $stateStore = new ImportStateStoreService($tempFileService, new SessionStore());

        $stateStore->setState('token_a', ['token' => 'token_a', 'created_at' => time(), 'path' => '/tmp/a.csv']);
        $loaded = $stateStore->getState('token_a');
        $this->assertIsArray($loaded);
        $this->assertSame('token_a', $loaded['token']);

        $stateStore->clearState('token_a');
        $this->assertNull($stateStore->getState('token_a'));
    }

    public function testExpiredStateGetsDeletedAndReturnsNull(): void
    {
        $tempFileService = $this->createMock(ImportTempFileService::class);
        $tempFileService->expects($this->once())
            ->method('delete')
            ->with('/tmp/expired.csv');

        $stateStore = new ImportStateStoreService($tempFileService, new SessionStore());
        $stateStore->setState('token_b', [
            'token' => 'token_b',
            'created_at' => time() - 7201,
            'path' => '/tmp/expired.csv',
        ]);

        $this->assertNull($stateStore->getState('token_b'));
    }
}
