<?php

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

class CommissionTest extends TestCase
{
    use DatabaseTransactions;

    protected $indexUrl = "/commissions";
    protected $item = "commission";
    protected $model = "App\Models\Commission";
    protected $table = "commissions";

    protected function indexTitle()
    {
        return "Commission";
    }

    protected function editTitle($text)
    {
        return "Commission - edit";
    }

    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->transfered_at->setTimezone('Asia/Manila'), 'transferedAt')
            ->select($item->booking_id, 'booking')
            ->type($item->amount, 'amount')
            ->select($item->currency_id, 'currency')
            ->type($item->comment, 'comment')

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

        $this->seeInDatabase($this->table ?? $this->item, [
            'transfered_at' => $item->transfered_at->setTimezone('UTC'),
            'booking_id' => $item->booking_id,
            'amount' => $item->amount,
            'currency_id' => $item->currency_id,
            'comment' => $item->comment
        ]);
    }

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

            ->seeInField('transferedAt', $item->transfered_at->setTimezone('Asia/Manila'))
            ->seeIsSelected('booking', $item->booking_id)
            ->seeInField('amount', $item->amount)
            ->seeIsSelected('currency', $item->currency_id)
            ->seeInField('comment', $item->comment)

            ->type($newItem->transfered_at->setTimezone('Asia/Manila'), 'transferedAt')
            ->select($newItem->booking_id, 'booking')
            ->type($newItem->amount, 'amount')
            ->select($newItem->currency_id, 'currency')
            ->type($newItem->comment, 'comment')

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

        $this->seeInDatabase($this->table ?? $this->item, [
            'transfered_at' => $newItem->transfered_at->setTimezone('UTC'),
            'booking_id' => $newItem->booking_id,
            'amount' => $newItem->amount,
            'currency_id' => $newItem->currency_id,
            'comment' => $newItem->comment
        ]);
    }

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

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

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