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

use PHPUnit\Framework\TestCase;
use admin\Controllers\NewsletterController;
use Domain\Newsletter\NewsletterRepository;
use Domain\Newsletter\NewsletterPreviewRenderer;

class NewsletterControllerTest extends TestCase
{
    private $repository;
    private $previewRenderer;
    private $controller;

    protected function setUp(): void
    {
        $this->repository = $this->createMock(NewsletterRepository::class);
        $this->previewRenderer = $this->createMock(NewsletterPreviewRenderer::class);
        $this->controller = new NewsletterController($this->repository, $this->previewRenderer);
    }

    public function testConstructorAcceptsDependencies(): void
    {
        $controller = new NewsletterController($this->repository, $this->previewRenderer);
        $this->assertInstanceOf(NewsletterController::class, $controller);
    }

    public function testHasMainActionMethods(): void
    {
        $this->assertTrue(method_exists($this->controller, 'emails_list'));
        $this->assertTrue(method_exists($this->controller, 'prepare'));
        $this->assertTrue(method_exists($this->controller, 'send'));
        $this->assertTrue(method_exists($this->controller, 'preview'));
        $this->assertTrue(method_exists($this->controller, 'settings'));
        $this->assertTrue(method_exists($this->controller, 'settings_save'));
        $this->assertTrue(method_exists($this->controller, 'email_templates_user'));
        $this->assertTrue(method_exists($this->controller, 'email_templates_admin'));
        $this->assertTrue(method_exists($this->controller, 'email_template_edit'));
        $this->assertTrue(method_exists($this->controller, 'template_save'));
        $this->assertTrue(method_exists($this->controller, 'email_template_delete'));
    }

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

        $this->assertEquals('string', (string)$reflection->getMethod('emails_list')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('prepare')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('send')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('preview')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('settings')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('settings_save')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('email_templates_user')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('email_templates_admin')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('email_template_edit')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('template_save')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('email_template_delete')->getReturnType());
    }

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

        $this->assertCount(2, $params);
        $this->assertEquals('Domain\Newsletter\NewsletterRepository', $params[0]->getType()->getName());
        $this->assertEquals('Domain\Newsletter\NewsletterPreviewRenderer', $params[1]->getType()->getName());
    }
}