<?php

namespace MintyPHP\Tests\Service\User;

use MintyPHP\Repository\User\UserLifecyclePolicyDashboardRepositoryInterface;
use MintyPHP\Service\User\UserLifecyclePolicyDashboardService;
use MintyPHP\Service\User\UserSettingsGateway;
use PHPUnit\Framework\TestCase;

class UserLifecyclePolicyDashboardServiceTest extends TestCase
{
    private function service(
        ?UserLifecyclePolicyDashboardRepositoryInterface $repository = null,
        ?UserSettingsGateway $settingsGateway = null
    ): UserLifecyclePolicyDashboardService {
        return new UserLifecyclePolicyDashboardService(
            $repository ?? $this->createMock(UserLifecyclePolicyDashboardRepositoryInterface::class),
            $settingsGateway ?? $this->createMock(UserSettingsGateway::class)
        );
    }

    public function testReturnsZeroWhenDeactivateDaysIsZero(): void
    {
        $repository = $this->createMock(UserLifecyclePolicyDashboardRepositoryInterface::class);
        $repository->expects($this->never())->method('countPendingDeletion');

        $this->assertSame(0, $this->service($repository)->pendingDeletionCount(0, 365, 7));
    }

    public function testReturnsZeroWhenDeleteDaysIsZero(): void
    {
        $repository = $this->createMock(UserLifecyclePolicyDashboardRepositoryInterface::class);
        $repository->expects($this->never())->method('countPendingDeletion');

        $this->assertSame(0, $this->service($repository)->pendingDeletionCount(180, 0, 7));
    }

    public function testReturnsZeroWhenWindowDaysIsZeroOrNegative(): void
    {
        $repository = $this->createMock(UserLifecyclePolicyDashboardRepositoryInterface::class);
        $repository->expects($this->never())->method('countPendingDeletion');

        $service = $this->service($repository);

        $this->assertSame(0, $service->pendingDeletionCount(180, 365, 0));
        $this->assertSame(0, $service->pendingDeletionCount(180, 365, -1));
    }

    public function testDelegatesToRepositoryWhenAllPolicyValuesPositive(): void
    {
        $repository = $this->createMock(UserLifecyclePolicyDashboardRepositoryInterface::class);
        $repository->expects($this->once())
            ->method('countPendingDeletion')
            ->with(365, 7)
            ->willReturn(42);

        $this->assertSame(42, $this->service($repository)->pendingDeletionCount(180, 365, 7));
    }

    public function testReturnsRepositoryCountVerbatim(): void
    {
        $repository = $this->createMock(UserLifecyclePolicyDashboardRepositoryInterface::class);
        $repository->method('countPendingDeletion')->willReturn(0);

        $this->assertSame(0, $this->service($repository)->pendingDeletionCount(180, 365, 7));
    }

    public function testLastRunReturnsNullWhenSettingsHaveNoRecord(): void
    {
        $settingsGateway = $this->createMock(UserSettingsGateway::class);
        $settingsGateway->expects($this->once())
            ->method('getLifecycleLastRun')
            ->willReturn(null);

        $this->assertNull($this->service(null, $settingsGateway)->lastRun());
    }

    public function testLastRunReturnsRecordedSettingsVerbatim(): void
    {
        $settingsGateway = $this->createMock(UserSettingsGateway::class);
        $settingsGateway->method('getLifecycleLastRun')->willReturn([
            'created_at' => '2026-04-26 17:30:00',
            'status' => 'success',
        ]);

        $row = $this->service(null, $settingsGateway)->lastRun();

        $this->assertIsArray($row);
        $this->assertSame('2026-04-26 17:30:00', $row['created_at']);
        $this->assertSame('success', $row['status']);
    }
}
