<?php

namespace Tests\Unit\Framework\Scheduler;

use Openguru\OpenCartFramework\Scheduler\Job;
use Openguru\OpenCartFramework\Scheduler\ScheduleJobRegistry;
use Tests\TestCase;

class ScheduleJobRegistryTest extends TestCase
{
    public function testRegistryCreation()
    {
        // Arrange & Act
        $registry = new ScheduleJobRegistry($this->app);

        // Assert
        $this->assertInstanceOf(ScheduleJobRegistry::class, $registry);
        $this->assertEmpty($registry->getJobs());
    }

    public function testAddJobWithoutName()
    {
        // Arrange
        $registry = new ScheduleJobRegistry($this->app);

        // Act
        $job = $registry->add(function() {});

        // Assert
        $this->assertInstanceOf(Job::class, $job);
        $this->assertCount(1, $registry->getJobs());
        $this->assertEquals('Closure', $job->getName());
    }

    public function testAddJobWithName()
    {
        // Arrange
        $registry = new ScheduleJobRegistry($this->app);

        // Act
        $job = $registry->add(function() {}, 'MyCustomJob');

        // Assert
        $this->assertInstanceOf(Job::class, $job);
        $this->assertCount(1, $registry->getJobs());
        $this->assertEquals('MyCustomJob', $job->getName());
    }

    public function testAddMultipleJobs()
    {
        // Arrange
        $registry = new ScheduleJobRegistry($this->app);

        // Act
        $job1 = $registry->add(function() {}, 'Job1');
        $job2 = $registry->add(function() {}, 'Job2');
        $job3 = $registry->add(TestTask::class, 'Job3');

        // Assert
        $jobs = $registry->getJobs();
        $this->assertCount(3, $jobs);
        $this->assertEquals('Job1', $jobs[0]->getName());
        $this->assertEquals('Job2', $jobs[1]->getName());
        $this->assertEquals('Job3', $jobs[2]->getName());
    }

    public function testGetJobsReturnsArray()
    {
        // Arrange
        $registry = new ScheduleJobRegistry($this->app);
        $registry->add(function() {}, 'TestJob');

        // Act
        $jobs = $registry->getJobs();

        // Assert
        $this->assertIsArray($jobs);
        $this->assertCount(1, $jobs);
        $this->assertInstanceOf(Job::class, $jobs[0]);
    }

    public function testJobSchedulingMethods()
    {
        // Arrange
        $registry = new ScheduleJobRegistry($this->app);

        // Act
        $job = $registry->add(function() {}, 'TestJob')
            ->everyFiveMinutes();

        // Assert
        $this->assertEquals('*/5 * * * *', $job->getExpression());
        // Just check that isDue() returns boolean, don't check the exact value
        // as it depends on current time when test runs
        $this->assertIsBool($job->isDue());
    }
}
