<?php

namespace Tests\Feature\Mailgateway;

use App\Mailgateway\Types\LocalType;
use App\Mailgateway\Types\MailmanType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Livewire\Livewire;
use Modules\Mailgateway\Components\Form;
use Tests\RequestFactories\MailmanTypeRequest;
use Tests\TestCase;

uses(DatabaseTransactions::class);
uses(TestCase::class);

it('test it saves a mail gateway', function () {
    test()->withoutExceptionHandling()->login()->loginNami();

    Livewire::test(Form::class)
        ->set('name', 'lala')
        ->set('domain', 'example.com')
        ->set('cls', LocalType::class)
        ->call('onSave')
        ->assertDispatched('closeModal')
        ->assertDispatched('refresh-page')
        ->assertDispatched('success');

    $this->assertDatabaseHas('mailgateways', [
        'domain' => 'example.com',
        'name' => 'lala',
        'type' => json_encode([
            'cls' => LocalType::class,
            'params' => [],
        ]),
    ]);
});

it('validates type', function () {
    test()->withoutExceptionHandling()->login()->loginNami();

    Livewire::test(Form::class)
        ->set('cls', '')
        ->assertHasErrors(['cls' => 'required']);
});

it('test it validates mail gateway', function (array $attributes, array $errors) {
    test()->withoutExceptionHandling()->login()->loginNami();

    Livewire::test(Form::class)
        ->set('name', 'lala')
        ->set('domain', 'example.com')
        ->set('cls', LocalType::class)
        ->setArray($attributes)
        ->call('onSave')
        ->assertHasErrors($errors)
        ->assertNotDispatched('closeModal')
        ->assertNotDispatched('refresh-page')
        ->assertNotDispatched('success');
})->with([
    [['name' => ''], ['name' => 'required']],
    [['domain' => ''], ['domain' => 'required']],
]);

it('test it validates mailman type', function (array $attributes, array $errors) {
    test()->withoutExceptionHandling()->login()->loginNami();

    Livewire::test(Form::class)
        ->set('name', 'lala')
        ->set('domain', 'example.com')
        ->set('cls', MailmanType::class)
        ->set('params.url', 'exampl.com')
        ->set('params.user', '::user::')
        ->set('params.password', 'password')
        ->setArray($attributes)
        ->call('onSave')
        ->assertHasErrors($errors)
        ->assertNotDispatched('closeModal');
})->with([
    [['params.url' => ''], ['params.url' => 'required']],
    [['params.user' => ''], ['params.user' => 'required']],
    [['params.password' => ''], ['params.password' => 'required']],
    [['params.owner' => ''], ['params.owner' => 'required']],
    [['params.owner' => 'aaa'], ['params.owner' => 'email']],
]);

it('test it stores mailman gateway', function () {
    test()->withoutExceptionHandling()->login()->loginNami();

    $typeParams = MailmanTypeRequest::new()->succeeds()->create(['url' => 'https://example.com', 'user' => 'user', 'password' => 'secret', 'owner' => 'owner@example.com']);

    Livewire::test(Form::class)
        ->setArray([
            'name' => 'lala',
            'domain' => 'https://example.com',
            'cls' => MailmanType::class,
            'params' => $typeParams
        ])
        ->call('onSave')
        ->assertDispatched('closeModal');

    $this->assertDatabaseHas('mailgateways', [
        'type' => json_encode([
            'cls' => MailmanType::class,
            'params' => $typeParams,
        ]),
        'name' => 'lala',
        'domain' => 'https://example.com',
    ]);
});

it('test it checks mailman connection', function () {
    test()->withoutExceptionHandling()->login()->loginNami();

    $typeParams = MailmanTypeRequest::new()->fails()->create(['url' => 'https://example.com', 'user' => 'user', 'password' => 'secret', 'owner' => 'owner@example.com']);

    Livewire::test(Form::class)
        ->setArray([
            'name' => 'lala',
            'domain' => 'https://example.com',
            'cls' => MailmanType::class,
            'params' => $typeParams
        ])
        ->call('onSave')
        ->assertHasErrors('connection')
        ->assertNotDispatched('closeModal');

    $this->assertDatabaseCount('mailgateways', 0);
});