<?php

namespace Modules\Module;

use App\Setting\NamiSettings;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Livewire\Livewire;
use Modules\Nami\Components\SettingView;
use Tests\TestCase;
use Zoomyboy\LaravelNami\Authentication\Auth;
use Zoomyboy\LaravelNami\Fakes\LoginFake;

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

it('it renders page', function () {
    test()->withoutExceptionHandling()->login()->loginNami();

    test()->get(route('setting.nami'))->assertSeeLivewire(SettingView::class);
});

it('it displays active credentials', function () {
    test()->withoutExceptionHandling()->login()->loginNami();
    app(NamiSettings::class)->fill(['mglnr' => 903, 'password' => 'secret', 'default_group_id' => 55])->save();

    Livewire::test(SettingView::class)
        ->assertSet('mglnr', 903)
        ->assertSet('password', '')
        ->assertSet('default_group_id', 55)
        ->assertSee('Mitgliedsnummer')
        ->assertSee('Standard-Gruppierung')
        ->assertSee('Passwort')
        ->assertDontSee('secret');
});

it('it saves credentials', function () {
    test()->withoutExceptionHandling()->login()->loginNami();
    Auth::success(100, 'secretneu');
    app(NamiSettings::class)->fill(['mglnr' => 903, 'password' => 'secret', 'default_group_id' => 55])->save();

    Livewire::test(SettingView::class)
        ->set('mglnr', 100)
        ->set('password', 'secretneu')
        ->set('default_group_id', 80)
        ->call('save')
        ->assertHasNoErrors()
        ->assertDispatched('success', 'Einstellungen gespeichert.');

    $this->assertEquals(100, app(NamiSettings::class)->mglnr);
    $this->assertEquals('secretneu', app(NamiSettings::class)->password);
    $this->assertEquals('80', app(NamiSettings::class)->default_group_id);
});

it('validates fields', function ($name, $value, $errors) {
    test()->withoutExceptionHandling()->login()->loginNami();
    Auth::success(100, 'secretneu');
    app(NamiSettings::class)->fill(['mglnr' => 903, 'password' => 'secret', 'default_group_id' => 55])->save();

    Livewire::test(SettingView::class)
        ->set('mglnr', 100)
        ->set('password', 'secretneu')
        ->set('default_group_id', 80)
        ->set($name, $value)
        ->call('save')
        ->assertHasErrors($errors)
        ->assertNotDispatched('success');
})->with([
    ['password', '', ['password' => 'required']],
    ['mglnr', '', ['mglnr' => 'required']],
    ['default_group_id', '', ['default_group_id' => 'required']],
    ['default_group_id', null, ['default_group_id' => 'required']],
]);

it('it throws error when saving failed', function () {
    test()->withoutExceptionHandling()->login()->loginNami();
    app(NamiSettings::class)->fill(['mglnr' => 903, 'password' => 'secret', 'default_group_id' => 55])->save();

    Livewire::test(SettingView::class)
        ->set('mglnr', 100)
        ->set('password', 'secretneu')
        ->set('default_group_id', 80)
        ->call('save')
        ->assertHasErrors(['mglnr' => 'Login fehlgeschlagen.'])
        ->assertNotDispatched('success');

    $settings = app(NamiSettings::class)->refresh();
    $this->assertEquals(903, $settings->mglnr);
    $this->assertEquals('secret', $settings->password);
    $this->assertEquals('55', $settings->default_group_id);
});