<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class PhilcareTest extends TestCase
{
    use DatabaseTransactions;

    protected $indexUrl = '/wages/philcare';
    protected $item = "philcare";
    protected $model = "App\Models\Wages\Philcare";
    protected $table = "wages_ph_philcare";

    protected function editTitle($name)
    {
        return "Edit philcare";
    }

    public function testUserCanGotoManagementPage()
    {
        $user = factory(\App\User::class)->create();
        $item = factory($this->model)->create();

        $this->actingAs($user)
            ->visit($this->indexUrl)
            ->see($this->indexTitle())
            ->see($item->name);
    }

    public function testUserCanCreateItem()
    {
        $user = factory(\App\User::class)->create();
        $item = factory($this->model)->make();

        $this->actingAs($user)
            ->visit($this->indexUrl)
            ->click($this->createButton())
            ->see($this->createTitle())

            ->type($item->range_from, 'range_from')
            ->type($item->range_until, 'range_until')
            ->type($item->contribution_ee, 'contribution_ee')
            ->type($item->contribution_er, 'contribution_er')

            ->press($this->createButton())
            ->see($this->indexTitle())
            ->see($item->name);

        $this->seeInDatabase($this->table ?? $this->item, [
            'range_from' => $item->range_from,
            'range_until' => $item->range_until,
            'contribution_ee' => $item->contribution_ee,
            'contribution_er' => $item->contribution_er
        ]);
    }

    public function testUserCanEditItem()
    {
        $user = factory(\App\User::class)->create();
        $item = factory($this->model)->create();
        $newItem = factory($this->model)->make();

        $this->actingAs($user)
            ->visit($this->indexUrl)
            ->within("tr:contains('$item->range_from')", function() { $this->click('edit'); })
            ->see($this->editTitle(""))

            ->seeInField('range_from', $item->range_from)
            ->seeInField('range_until', $item->range_until)
            ->seeInField('contribution_ee', $item->contribution_ee)
            ->seeInField('contribution_er', $item->contribution_er)

            ->type($newItem->range_from, 'range_from')
            ->type($newItem->range_until, 'range_until')
            ->type($newItem->contribution_ee, 'contribution_ee')
            ->type($newItem->contribution_er, 'contribution_er')

            ->press($this->updateButton())
            ->see($this->indexTitle())
            ->see($newItem->name);

        $this->seeInDatabase($this->table ?? $this->item, [
            'range_from' => $newItem->range_from,
            'range_until' => $newItem->range_until,
            'contribution_ee' => $newItem->contribution_ee,
            'contribution_er' => $newItem->contribution_er
        ]);
    }

    public function testUserCanDeleteItem()
    {
        $user = factory(\App\User::class)->create();
        $item = factory($this->model)->create();

        $this->actingAs($user)
            ->visit($this->indexUrl)
            ->within("tr:contains('$item->range_from')", function() { $this->press('delete'); })
            ->see($this->indexTitle())
            ->dontSee($item->range_from);

        $this->dontSeeInDatabase($this->table ?? $this->item, [
            'id' => $item->id
        ]);
    }
}
