<?php

namespace Tests\Unit\Framework\Scheduler;

use Openguru\OpenCartFramework\Scheduler\SchedulerResult;
use Tests\TestCase;

class SchedulerResultTest extends TestCase
{
    public function testSchedulerResultInitialization()
    {
        // Arrange & Act
        $result = new SchedulerResult();

        // Assert
        $this->assertEmpty($result->executed);
        $this->assertEmpty($result->failed);
        $this->assertEmpty($result->skipped);
    }

    public function testAddExecuted()
    {
        // Arrange
        $result = new SchedulerResult();

        // Act
        $result->addExecuted('TestJob', 1.5);

        // Assert
        $this->assertCount(1, $result->executed);
        $this->assertEquals('TestJob', $result->executed[0]['name']);
        $this->assertEquals(1.5, $result->executed[0]['duration']);
    }

    public function testAddFailed()
    {
        // Arrange
        $result = new SchedulerResult();

        // Act
        $result->addFailed('TestJob', 'Database connection failed');

        // Assert
        $this->assertCount(1, $result->failed);
        $this->assertEquals('TestJob', $result->failed[0]['name']);
        $this->assertEquals('Database connection failed', $result->failed[0]['error']);
    }

    public function testAddSkipped()
    {
        // Arrange
        $result = new SchedulerResult();

        // Act
        $result->addSkipped('TestJob', 'Not due yet');

        // Assert
        $this->assertCount(1, $result->skipped);
        $this->assertEquals('TestJob', $result->skipped[0]['name']);
        $this->assertEquals('Not due yet', $result->skipped[0]['reason']);
    }

    public function testMultipleOperations()
    {
        // Arrange
        $result = new SchedulerResult();

        // Act
        $result->addExecuted('Job1', 0.5);
        $result->addExecuted('Job2', 1.2);
        $result->addFailed('Job3', 'Timeout');
        $result->addSkipped('Job4', 'Already running');
        $result->addSkipped('Job5', 'Not due');

        // Assert
        $this->assertCount(2, $result->executed);
        $this->assertCount(1, $result->failed);
        $this->assertCount(2, $result->skipped);
    }

    public function testToArray()
    {
        // Arrange
        $result = new SchedulerResult();
        $result->addExecuted('Job1', 1.0);
        $result->addFailed('Job2', 'Error');
        $result->addSkipped('Job3', 'Skipped');

        // Act
        $array = $result->toArray();

        // Assert
        $expected = [
            'executed' => [['name' => 'Job1', 'duration' => 1.0]],
            'failed' => [['name' => 'Job2', 'error' => 'Error']],
            'skipped' => [['name' => 'Job3', 'reason' => 'Skipped']],
        ];
        $this->assertEquals($expected, $array);
    }
}
