<?php
namespace Tests\Unit\admin\Controllers;

use admin\Controllers\PagesController;
use Domain\Languages\LanguagesRepository;
use Domain\Layouts\LayoutsRepository;
use Domain\Pages\PagesRepository;
use PHPUnit\Framework\TestCase;

class PagesControllerTest extends TestCase
{
    private $mockRepository;
    private $mockLanguagesRepository;
    private $mockLayoutsRepository;
    private $controller;

    protected function setUp(): void
    {
        $this->mockRepository = $this->createMock(PagesRepository::class);
        $this->mockLanguagesRepository = $this->createMock(LanguagesRepository::class);
        $this->mockLayoutsRepository = $this->createMock(LayoutsRepository::class);

        $this->controller = new PagesController(
            $this->mockRepository,
            $this->mockLanguagesRepository,
            $this->mockLayoutsRepository
        );
    }

    public function testConstructorAcceptsRepositories(): void
    {
        $controller = new PagesController(
            $this->mockRepository,
            $this->mockLanguagesRepository,
            $this->mockLayoutsRepository
        );

        $this->assertInstanceOf(PagesController::class, $controller);
    }

    public function testHasMainActionMethods(): void
    {
        $this->assertTrue(method_exists($this->controller, 'list'));
        $this->assertTrue(method_exists($this->controller, 'edit'));
        $this->assertTrue(method_exists($this->controller, 'save'));
        $this->assertTrue(method_exists($this->controller, 'delete'));
        $this->assertTrue(method_exists($this->controller, 'menuEdit'));
        $this->assertTrue(method_exists($this->controller, 'menuSave'));
        $this->assertTrue(method_exists($this->controller, 'menuDelete'));
        $this->assertTrue(method_exists($this->controller, 'savePagesOrder'));
        $this->assertTrue(method_exists($this->controller, 'saveArticlesOrder'));
        $this->assertTrue(method_exists($this->controller, 'generateSeoLink'));
    }

    public function testActionMethodReturnTypes(): void
    {
        $reflection = new \ReflectionClass($this->controller);

        $this->assertEquals('string', (string)$reflection->getMethod('list')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('edit')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('save')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('delete')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('menuEdit')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('menuSave')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('menuDelete')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('savePagesOrder')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('saveArticlesOrder')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('generateSeoLink')->getReturnType());
    }

    public function testConstructorRequiresPagesLanguagesAndLayoutsRepositories(): void
    {
        $reflection = new \ReflectionClass(PagesController::class);
        $constructor = $reflection->getConstructor();
        $params = $constructor->getParameters();

        $this->assertCount(3, $params);
        $this->assertEquals('Domain\Pages\PagesRepository', $params[0]->getType()->getName());
        $this->assertEquals('Domain\Languages\LanguagesRepository', $params[1]->getType()->getName());
        $this->assertEquals('Domain\Layouts\LayoutsRepository', $params[2]->getType()->getName());
    }
}
