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

use PHPUnit\Framework\TestCase;
use Domain\Layouts\LayoutsRepository;

class LayoutsRepositoryTest extends TestCase
{
    public function testFindReturnsLayoutWithRelations(): void
    {
        $mockDb = $this->createMock(\medoo::class);

        $mockDb->expects($this->once())
            ->method('get')
            ->with('pp_layouts', '*', ['id' => 5])
            ->willReturn(['id' => 5, 'name' => 'Main']);

        $mockDb->expects($this->exactly(2))
            ->method('select')
            ->withConsecutive(
                ['pp_layouts_pages', 'page_id', ['layout_id' => 5]],
                ['pp_layouts_categories', 'category_id', ['layout_id' => 5]]
            )
            ->willReturnOnConsecutiveCalls([10, 11], [2, 3]);

        $repository = new LayoutsRepository($mockDb);
        $layout = $repository->find(5);

        $this->assertSame(5, $layout['id']);
        $this->assertSame([10, 11], $layout['pages']);
        $this->assertSame([2, 3], $layout['categories']);
    }

    public function testDeleteReturnsFalseWhenOnlyOneLayoutExists(): void
    {
        $mockDb = $this->createMock(\medoo::class);

        $mockDb->expects($this->once())
            ->method('count')
            ->with('pp_layouts')
            ->willReturn(1);

        $repository = new LayoutsRepository($mockDb);
        $this->assertFalse($repository->delete(1));
    }

    public function testFindReturnsDefaultLayoutWhenRecordDoesNotExist(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $mockDb->expects($this->once())
            ->method('get')
            ->with('pp_layouts', '*', ['id' => 999])
            ->willReturn(false);

        $repository = new LayoutsRepository($mockDb);
        $layout = $repository->find(999);

        $this->assertSame(0, $layout['id']);
        $this->assertSame([], $layout['pages']);
        $this->assertSame([], $layout['categories']);
    }

    public function testSaveInsertsNewLayoutAndReturnsId(): void
    {
        $mockDb = $this->createMock(\medoo::class);

        $mockDb->expects($this->once())
            ->method('insert')
            ->with('pp_layouts', $this->arrayHasKey('name'));

        $mockDb->expects($this->once())
            ->method('id')
            ->willReturn(9);

        $mockDb->expects($this->exactly(2))
            ->method('delete')
            ->withConsecutive(
                ['pp_layouts_pages', ['layout_id' => 9]],
                ['pp_layouts_categories', ['layout_id' => 9]]
            )
            ->willReturn(true);

        $repository = new LayoutsRepository($mockDb);
        $savedId = $repository->save([
            'name' => 'Nowy szablon',
            'status' => 0,
            'categories_default' => 0,
        ]);

        $this->assertSame(9, $savedId);
    }

    public function testListAllReturnsArray(): void
    {
        $mockDb = $this->createMock(\medoo::class);

        $mockDb->expects($this->once())
            ->method('select')
            ->with('pp_layouts', '*', ['ORDER' => ['name' => 'ASC']])
            ->willReturn([
                ['id' => 1, 'name' => 'Default'],
            ]);

        $repository = new LayoutsRepository($mockDb);
        $rows = $repository->listAll();

        $this->assertCount(1, $rows);
        $this->assertSame('Default', $rows[0]['name']);
    }
}
