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

use Domain\Pages\PagesRepository;
use PHPUnit\Framework\TestCase;

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

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

        $repository = new PagesRepository($mockDb);
        $menus = $repository->menusList();

        $this->assertCount(1, $menus);
        $this->assertSame(1, (int)$menus[0]['id']);
        $this->assertSame('Main', (string)$menus[0]['name']);
    }

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

        $mockDb->expects($this->once())
            ->method('count')
            ->with('pp_pages', ['menu_id' => 3])
            ->willReturn(2);

        $mockDb->expects($this->never())->method('delete');

        $repository = new PagesRepository($mockDb);
        $this->assertFalse($repository->menuDelete(3));
    }

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

        $mockDb->expects($this->exactly(4))
            ->method('count')
            ->willReturnOnConsecutiveCalls(1, 0, 0, 0);

        $repository = new PagesRepository($mockDb);
        $seoLink = $repository->generateSeoLink('test title', 0, 0, 0);

        $this->assertSame('test title-1', $seoLink);
    }

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

        $url = $repository->pageUrlPreview(5, 'en', 'About us', 'about-us', 'pl');

        $this->assertSame('/en/about-us', $url);
    }
}
