<?php

namespace MintyPHP\Tests\Service\Scheduler;

use MintyPHP\Service\Scheduler\Handler\UserLifecycleJobHandler;
use MintyPHP\Service\User\UserLifecycleService;
use PHPUnit\Framework\TestCase;

class UserLifecycleJobHandlerTest extends TestCase
{
    public function testExecuteReturnsSuccessWithMappedResult(): void
    {
        $lifecycleService = $this->createMock(UserLifecycleService::class);
        $lifecycleService->expects($this->once())
            ->method('run')
            ->with(42)
            ->willReturn([
                'ok' => true,
                'run_uuid' => 'run-1',
                'deactivated_count' => 3,
                'deleted_count' => 2,
                'skipped_count' => 1,
                'duration_ms' => 99,
            ]);

        $handler = new UserLifecycleJobHandler($lifecycleService);
        $result = $handler->execute(42);

        $this->assertSame('success', $result['status']);
        $this->assertNull($result['error_code']);
        $this->assertSame('run-1', $result['result']['run_uuid']);
        $this->assertSame(3, $result['result']['deactivated_count']);
        $this->assertSame(2, $result['result']['deleted_count']);
        $this->assertSame(1, $result['result']['skipped_count']);
        $this->assertSame(99, $result['result']['duration_ms']);
    }

    public function testExecuteReturnsSkippedWhenLifecycleLockIsNotAcquired(): void
    {
        $lifecycleService = $this->createMock(UserLifecycleService::class);
        $lifecycleService->expects($this->once())
            ->method('run')
            ->with(null)
            ->willReturn([
                'ok' => false,
                'error' => 'lock_not_acquired',
            ]);

        $handler = new UserLifecycleJobHandler($lifecycleService);
        $result = $handler->execute(null);

        $this->assertSame('skipped', $result['status']);
        $this->assertSame('lock_not_acquired', $result['error_code']);
    }
}
