<?php

namespace Tests\Feature\Controllers;

use App\Http\Controllers\AppController;
use Illuminate\Support\Facades\Config;
use ReflectionClass;
use Tests\TestCase;

class AppControllerTest extends TestCase
{
    /** @test */
    public function it_redirects_to_home_when_no_email_exists_in_mailbox(): void
    {
        $response = $this->get('/mailbox');

        $response->assertRedirect('/');
    }

    /** @test */
    public function it_creates_custom_email_from_url_when_enabled(): void
    {
        $email = 'custom@example.com';

        $response = $this->get("/mailbox/{$email}");

        $response->assertRedirect('/mailbox');
    }

    /** @test */
    public function it_validates_email_parameter_in_mailbox_route(): void
    {
        $response = $this->get('/mailbox/invalid-email');

        $response->assertStatus(302); // Validation redirects back
        $response->assertSessionHasErrors();
    }

    /** @test */
    public function it_redirects_to_home_when_mailbox_slug_is_disabled(): void
    {
        Config::set('app.settings.configuration_settings', json_encode([
            'disable_mailbox_slug' => true,
        ]));

        $response = $this->get('/mailbox');

        $response->assertRedirect('/');
    }

    /** @test */
    public function it_switches_email_successfully(): void
    {
        $email = 'newemail@example.com';

        $response = $this->get("/switch/{$email}");

        $response->assertRedirect('/mailbox');
    }

    /** @test */
    public function it_redirects_to_home_when_switching_email_with_disabled_mailbox_slug(): void
    {
        Config::set('app.settings.configuration_settings', json_encode([
            'disable_mailbox_slug' => true,
        ]));

        $email = 'newemail@example.com';

        $response = $this->get("/switch/{$email}");

        $response->assertRedirect('/');
    }

    /** @test */
    public function it_deletes_email_successfully(): void
    {
        $email = 'delete@example.com';

        $response = $this->get("/delete/{$email}");

        $response->assertRedirect('/mailbox');
    }

    /** @test */
    public function it_redirects_to_home_when_deleting_email_without_parameter(): void
    {
        $response = $this->get('/delete');

        $response->assertRedirect('/');
    }

    /** @test */
    public function it_switches_locale_successfully(): void
    {
        $locale = 'es';

        $response = $this->get("/locale/{$locale}");

        $response->assertRedirect();
        $this->assertEquals($locale, session('locale'));
    }

    /** @test */
    public function it_aborts_with_400_for_invalid_locale(): void
    {
        $invalidLocale = 'invalid';

        $response = $this->get("/locale/{$invalidLocale}");

        $response->assertStatus(400);
    }

    /** @test */
    public function it_redirects_back_after_locale_switch(): void
    {
        $locale = 'fr';

        $response = $this->get("/locale/{$locale}");

        $response->assertRedirect();
        $this->assertEquals($locale, session('locale'));
    }

    /** @test */
    public function it_handles_get_string_between_method_correctly(): void
    {
        $controller = new AppController;
        $reflection = new ReflectionClass($controller);
        $method = $reflection->getMethod('getStringBetween');

        $string = 'Hello [world] test';
        $result = $method->invoke($controller, $string, '[', ']');

        $this->assertEquals('world', $result);
    }

    /** @test */
    public function it_handles_get_string_between_with_missing_end(): void
    {
        $controller = new AppController;
        $reflection = new ReflectionClass($controller);
        $method = $reflection->getMethod('getStringBetween');

        $string = 'Hello [world test';
        $result = $method->invoke($controller, $string, '[', ']');

        $this->assertEquals('wo', $result);
    }

    /** @test */
    public function it_handles_get_string_between_with_no_match(): void
    {
        $controller = new AppController;
        $reflection = new ReflectionClass($controller);
        $method = $reflection->getMethod('getStringBetween');

        $string = 'Hello world test';
        $result = $method->invoke($controller, $string, '[', ']');

        $this->assertEquals('', $result);
    }
}
