<?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);
    }

    // ── Frontend methods tests ──────────────────────────────────

    public function testFrontPageDetailsReturnsPageWithLanguage(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $mockDb->expects($this->exactly(2))
            ->method('get')
            ->willReturnOnConsecutiveCalls(
                ['id' => 5, 'menu_id' => 1, 'status' => 1, 'page_type' => 0],
                ['page_id' => 5, 'lang_id' => 'pl', 'title' => 'O nas', 'seo_link' => 'o-nas']
            );

        $repo = new PagesRepository($mockDb);
        $page = $repo->frontPageDetails(5, 'pl');

        $this->assertSame(5, $page['id']);
        $this->assertSame('O nas', $page['language']['title']);
    }

    public function testFrontPageDetailsReturnsNullWhenNotFound(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $mockDb->method('get')->willReturn(null);

        $repo = new PagesRepository($mockDb);
        $this->assertNull($repo->frontPageDetails(999, 'pl'));
    }

    public function testFrontMainPageIdReturnsStartPage(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $mockDb->expects($this->once())
            ->method('get')
            ->with('pp_pages', 'id', ['AND' => ['status' => 1, 'start' => 1]])
            ->willReturn(3);

        $repo = new PagesRepository($mockDb);
        $this->assertSame(3, $repo->frontMainPageId());
    }

    public function testFrontMainPageIdFallsBackToFirstActive(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $mockDb->expects($this->exactly(2))
            ->method('get')
            ->willReturnOnConsecutiveCalls(null, 7);

        $repo = new PagesRepository($mockDb);
        $this->assertSame(7, $repo->frontMainPageId());
    }

    public function testFrontPageSortReturnsValue(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $mockDb->expects($this->once())
            ->method('get')
            ->with('pp_pages', 'sort_type', ['id' => 5])
            ->willReturn(2);

        $repo = new PagesRepository($mockDb);
        $this->assertSame(2, $repo->frontPageSort(5));
    }

    public function testFrontMenuDetailsReturnsMenuWithPages(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $mockDb->expects($this->once())
            ->method('get')
            ->with('pp_menus', '*', ['id' => 1])
            ->willReturn(['id' => 1, 'name' => 'Main']);

        $mockDb->expects($this->once())
            ->method('select')
            ->willReturn([]);

        $repo = new PagesRepository($mockDb);
        $menu = $repo->frontMenuDetails(1, 'pl');

        $this->assertSame(1, $menu['id']);
        $this->assertSame('Main', $menu['name']);
        $this->assertSame([], $menu['pages']);
    }

    public function testFrontMenuDetailsReturnsNullForInvalidMenu(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $mockDb->expects($this->once())
            ->method('get')
            ->willReturn(null);

        $repo = new PagesRepository($mockDb);
        $this->assertNull($repo->frontMenuDetails(999, 'pl'));
    }

    public function testFrontMenuPagesReturnsEmptyForNoPages(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $mockDb->expects($this->once())
            ->method('select')
            ->willReturn([]);

        $repo = new PagesRepository($mockDb);
        $pages = $repo->frontMenuPages(1, 'pl');

        $this->assertSame([], $pages);
    }
}
