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

use PHPUnit\Framework\TestCase;
use Domain\Scontainers\ScontainersRepository;

class ScontainersRepositoryTest extends TestCase
{
    public function testFindReturnsDefaultContainerForInvalidId(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $repository = new ScontainersRepository($mockDb);

        $container = $repository->find(0);

        $this->assertIsArray($container);
        $this->assertSame(0, (int)$container['id']);
        $this->assertSame(1, (int)$container['status']);
    }

    public function testDeleteReturnsFalseForInvalidId(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $repository = new ScontainersRepository($mockDb);

        $this->assertFalse($repository->delete(0));
    }

    public function testFindReturnsContainerWithTranslations(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $mockDb->expects($this->once())
            ->method('get')
            ->with('pp_scontainers', '*', ['id' => 7])
            ->willReturn(['id' => 7, 'status' => 1, 'show_title' => 1]);

        $mockDb->expects($this->once())
            ->method('select')
            ->with('pp_scontainers_langs', '*', ['container_id' => 7])
            ->willReturn([
                ['lang_id' => 'pl', 'title' => 'Tytul PL', 'text' => 'Tekst PL'],
                ['lang_id' => 'en', 'title' => 'Title EN', 'text' => 'Text EN'],
            ]);

        $repository = new ScontainersRepository($mockDb);
        $container = $repository->find(7);

        $this->assertSame(7, (int)$container['id']);
        $this->assertArrayHasKey('languages', $container);
        $this->assertArrayHasKey('pl', $container['languages']);
        $this->assertArrayHasKey('en', $container['languages']);
    }

    public function testDetailsForLanguageReturnsNullForInvalidData(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $repository = new ScontainersRepository($mockDb);

        $this->assertNull($repository->detailsForLanguage(0, 'pl'));
        $this->assertNull($repository->detailsForLanguage(1, ''));
    }
}

