<?php

namespace Modules\Invoice;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Livewire\Livewire;
use Modules\Invoice\Components\SettingView;
use Tests\TestCase;

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

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

    test()->get('/setting/bill')->assertSeeLivewire(SettingView::class);
});

it('it displays settings', function () {
    $this->withoutExceptionHandling()->login()->loginNami();
    app(InvoiceSettings::class)->fill([
        'from_long' => 'DPSG Stamm Muster',
        'from' => 'Stamm Muster',
        'mobile' => '+49 176 55555',
        'email' => 'max@muster.de',
        'website' => 'https://example.com',
        'address' => 'Musterstr 4',
        'place' => 'Solingen',
        'zip' => '12345',
        'iban' => 'DE05',
        'bic' => 'SOLSDE',
        'rememberWeeks' => 6
    ])->save();

    Livewire::test(SettingView::class)
        ->assertSet('settings.from_long', 'DPSG Stamm Muster')
        ->assertSet('settings.from', 'Stamm Muster')
        ->assertSet('settings.mobile', '+49 176 55555')
        ->assertSet('settings.email', 'max@muster.de')
        ->assertSet('settings.website', 'https://example.com')
        ->assertSet('settings.address', 'Musterstr 4')
        ->assertSet('settings.place', 'Solingen')
        ->assertSet('settings.zip', '12345')
        ->assertSet('settings.iban', 'DE05')
        ->assertSet('settings.bic', 'SOLSDE')
        ->assertSet('settings.rememberWeeks', 6);
});

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

    Livewire::test(SettingView::class)
        ->set('settings.from_long', 'DPSG Stamm Muster')
        ->set('settings.from', 'Stamm Muster')
        ->set('settings.mobile', '+49 176 55555')
        ->set('settings.email', 'max@muster.de')
        ->set('settings.website', 'https://example.com')
        ->set('settings.address', 'Musterstr 4')
        ->set('settings.place', 'Solingen')
        ->set('settings.zip', '12345')
        ->set('settings.iban', 'DE05')
        ->set('settings.bic', 'SOLSDE')
        ->set('settings.rememberWeeks', 10)
        ->call('save')
        ->assertDispatched('success', 'Einstellungen gespeichert.');

    $settings = app(InvoiceSettings::class);
    $this->assertEquals('DPSG Stamm Muster', $settings->from_long);
    $this->assertEquals('DE05', $settings->iban);
    $this->assertEquals('SOLSDE', $settings->bic);
    $this->assertEquals(10, $settings->rememberWeeks);
});