<?php

namespace Tests\Feature\Mailgateway;

use App\Mailgateway\Models\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 Phake;
use Tests\RequestFactories\MailmanTypeRequest;
use Tests\TestCase;

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

it('test it sets attributes for mailman', function () {
    test()->withoutExceptionHandling()->login()->loginNami();

    $typeParams = MailmanTypeRequest::new()->create(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner']);
    $mailgateway = Mailgateway::factory()->type(MailmanType::class, $typeParams)->create(['name' => '::name::', 'domain' => 'example.com']);

    Livewire::test(Form::class, ['model' => $mailgateway->id])
        ->assertSet('id', $mailgateway->id)
        ->assertSet('name', '::name::')
        ->assertSet('domain', 'example.com')
        ->assertSet('cls', MailmanType::class)
        ->assertSet('params.url', 'https://mailman.example.com')
        ->assertSet('params.user', 'user')
        ->assertSet('params.password', 'password')
        ->assertSet('params.owner', 'owner');
});

it('test it sets attributes for local', function () {
    test()->withoutExceptionHandling()->login()->loginNami();

    $mailgateway = Mailgateway::factory()->type(LocalType::class, [])->create(['name' => '::name::', 'domain' => 'example.com']);

    Livewire::test(Form::class, ['model' => $mailgateway->id])
        ->assertSet('name', '::name::')
        ->assertSet('domain', 'example.com')
        ->assertSet('cls', LocalType::class)
        ->assertSet('params', []);
});

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

    $mailgateway = Mailgateway::factory()->type(LocalType::class, [])->create(['name' => '::name::', 'domain' => 'example.com']);

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

it('test it updates a mailman gateway without updating password', function () {
    test()->withoutExceptionHandling()->login()->loginNami();

    $typeParams = MailmanTypeRequest::new()->succeeds()->create(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner@example.com']);
    $mailgateway = Mailgateway::factory()->type(MailmanType::class, $typeParams)->create(['name' => '::name::', 'domain' => 'example.com']);

    Livewire::test(Form::class, ['model' => $mailgateway->id])
        ->set('name', '::newname::')
        ->call('onSave')
        ->assertHasNoErrors();

    $this->assertDatabaseCount('mailgateways', 1);
    $this->assertDatabaseHas('mailgateways', [
        'name' => '::newname::',
        'type' => json_encode(['cls' => MailmanType::class, 'params' => $typeParams]),
    ]);
});

it('test it updates a mailman gateway with password', function () {
    test()->withoutExceptionHandling()->login()->loginNami();

    $typeParams = MailmanTypeRequest::new()->create(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner@example.com']);
    $newTypeParams = MailmanTypeRequest::new()->succeeds()->create(['url' => 'https://mailman.example.com', 'user' => 'newuser', 'password' => 'password', 'owner' => 'owner@example.com']);
    $mailgateway = Mailgateway::factory()->type(MailmanType::class, $typeParams)->create();

    Livewire::test(Form::class, ['model' => $mailgateway->id])
        ->set('params.user', 'newuser')
        ->call('onSave')
        ->assertHasNoErrors();

    $this->assertDatabaseCount('mailgateways', 1);
    $this->assertDatabaseHas('mailgateways', [
        'type' => json_encode(['cls' => MailmanType::class, 'params' => $newTypeParams]),
    ]);
});

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

    $typeParams = MailmanTypeRequest::new()->create(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner@example.com']);
    MailmanTypeRequest::new()->fails()->create(['url' => 'https://mailman.example.com', 'user' => 'newuser', 'password' => 'password', 'owner' => 'owner@example.com']);
    $mailgateway = Mailgateway::factory()->type(MailmanType::class, $typeParams)->create();

    Livewire::test(Form::class, ['model' => $mailgateway->id])
        ->set('params.user', 'newuser')
        ->call('onSave')
        ->assertHasErrors('connection');
});