<?php
namespace Tests\Unit\Domain\Update;

use PHPUnit\Framework\TestCase;
use Domain\Update\UpdateRepository;

class UpdateRepositoryTest extends TestCase
{
    private function createMockDb(): \medoo
    {
        return $this->createMock(\medoo::class);
    }

    public function testConstructorAcceptsDb(): void
    {
        $db = $this->createMockDb();
        $repository = new UpdateRepository($db);
        $this->assertInstanceOf(UpdateRepository::class, $repository);
    }

    public function testHasUpdateMethod(): void
    {
        $db = $this->createMockDb();
        $repository = new UpdateRepository($db);
        $this->assertTrue(method_exists($repository, 'update'));
    }

    public function testUpdateReturnsArray(): void
    {
        $db = $this->createMockDb();
        $repository = new UpdateRepository($db);

        $reflection = new \ReflectionClass($repository);
        $method = $reflection->getMethod('update');
        $this->assertEquals('array', (string)$method->getReturnType());
    }

    public function testHasRunPendingMigrationsMethod(): void
    {
        $db = $this->createMockDb();
        $repository = new UpdateRepository($db);
        $this->assertTrue(method_exists($repository, 'runPendingMigrations'));
    }

    public function testRunPendingMigrationsWithNoResults(): void
    {
        $db = $this->createMockDb();
        $db->method('select')->willReturn(false);

        $repository = new UpdateRepository($db);
        $repository->runPendingMigrations();

        $this->assertTrue(true); // No exception thrown
    }

    public function testHasPrivateHelperMethods(): void
    {
        $reflection = new \ReflectionClass(UpdateRepository::class);

        $privateMethods = [
            'downloadAndApply',
            'executeSql',
            'deleteFiles',
            'extractZip',
            'saveLog',
        ];

        foreach ($privateMethods as $methodName) {
            $this->assertTrue(
                $reflection->hasMethod($methodName),
                "Missing private method: {$methodName}"
            );
            $method = $reflection->getMethod($methodName);
            $this->assertTrue(
                $method->isPrivate(),
                "Method {$methodName} should be private"
            );
        }
    }
}
