<?php

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

class SpecialTimeTest extends TestCase
{
    use DatabaseTransactions;

    protected $indexUrl = "/wages/specialtimes";
    protected $item = "special time";
    protected $model = "App\Models\Wages\SpecialTime";
    protected $table = "wages_ph_special_times";

    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())

            ->select($item->employee_id, 'employee_id')
            ->type($item->special_time_date, 'special_time_date')
            ->type($item->hours, 'hours')
            ->select($item->special_time_type_id, 'special_time_type_id')

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

        $this->seeInDatabase($this->table ?? $this->item, [
            'employee_id' => $item->employee_id,
            'special_time_date' => $item->special_time_date,
            'hours' => $item->hours,
            'special_time_type_id' => $item->special_time_type_id
        ]);
    }

    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->name')", function() { $this->click('edit'); })
            ->see($this->editTitle(""))

            ->seeIsSelected('employee_id', $item->employee_id)
            ->seeInField('special_time_date', $item->special_time_date->timezone('Asia/Manila'))
            ->seeInField('hours', $item->hours)
            ->seeIsSelected('special_time_type_id', $item->special_time_type_id)

            ->select($newItem->employee_id, 'employee_id')
            ->type($newItem->special_time_date, 'special_time_date')
            ->type($newItem->hours, 'hours')
            ->select($newItem->special_time_type_id, 'special_time_type_id')

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

        $this->seeInDatabase($this->table ?? $this->item, [
            'employee_id' => $newItem->employee_id,
            'special_time_date' => $newItem->special_time_date,
            'hours' => $newItem->hours,
            'special_time_type_id' => $newItem->special_time_type_id
        ]);
    }

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

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

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