<?php

use Laravel\Lumen\Testing\DatabaseMigrations;
use Laravel\Lumen\Testing\DatabaseTransactions;
use Tests\AuthHelper;

class MarkersTest extends TestCase
{
    use DatabaseTransactions;

    public function testGetAllMarkers()
    {
        $this->get('/markers');
        $this->seeStatusCode(200);
        $this->seeJsonStructure([
          [
            'id',
            'title',
            'latitude',
            'longitude',
            'type'
          ]
        ]);
    }

    public function testGetMarker()
    {
        $this->get('/markers/8');
        $this->seeStatusCode(200);
        $this->seeJsonStructure([
          'id',
          'title',
          'latitude',
          'longitude',
          'type',
          'bibliography',
          'description',
          'photos' => [
            [
              'id',
              'marker_id',
              'src',
              'thumbnail'
            ]
          ]
        ]);
        $this->seeJson([
          'id' => 8
        ]);
    }

    public function testGetNotExistingMarker()
    {
        $this->get('/markers/102487');
        $this->seeStatusCode(404);
        $this->seeJsonStructure([
          'error',
        ]);
        $this->seeJson([
          'error' => 'MarkerNotFound'
        ]);
    }

    public function testSearchMarkers()
    {
        $this->post('/markers/search', ['query' => 'Hrob koně']);
        $this->seeStatusCode(200);
        $this->seeJsonStructure([
          [
            'id',
            'title',
            'latitude',
            'longitude',
            'type'
          ]
        ]);
        $this->seeJson([
          [
            'id' => 2,
            'title' => 'Hrob koně',
            'latitude' => '48.7289903',
            'longitude' => '16.8965294',
            'type' => 'kultovni_objekt'
          ]
        ]);
    }

    public function testCreateNewMarker()
    {
        $authHelper = new AuthHelper();

        $this->post('/markers',
          [
           'title' => 'test',
           'latitude' => '12',
           'longitude' => '34',
           'type' => 'kultovni_objekt'
         ],
         $authHelper->getAuthHeader()
        );
        $this->seeStatusCode(201);
        $this->seeJson([
          'title' => 'test',
          'latitude' => 12,
          'longitude' => 34,
          'type' => 'kultovni_objekt'
        ]);
    }

    public function testCreateNewMarkerWithoutAuth()
    {
        $this->post('/markers');
        $this->seeStatusCode(401);
        $this->seeJson([
          'error' => 'Token not provided.'
        ]);
    }

    public function testDeleteMarker()
    {
        $authHelper = new AuthHelper();
        $this->delete('/markers/1',
          [],
          $authHelper->getAuthHeader()
        );
        $this->seeStatusCode(202);
        $this->seeJson([
          'success' => 'Marker was successfully deleted.'
        ]);
    }

    public function testDeleteMarkerWithoutAuth()
    {
        $this->delete('/markers/1');
        $this->seeStatusCode(401);
        $this->seeJson([
          'error' => 'Token not provided.'
        ]);
    }

    public function testUpdateMarker()
    {
        $authHelper = new AuthHelper();
        $this->post('/markers/1',
          ['title' => 'test1',
           'latitude' => '10',
           'longitude' => '20',
           'type' => 'kostel',
           'bibliography' => 'some books',
           'description' => 'some desc'
          ],
          $authHelper->getAuthHeader()
        );
        $this->seeStatusCode(200);
        $this->seeJson([
          'id' => 1,
          'title' => 'test1',
          'latitude' => 10,
          'longitude' => 20,
          'type' => 'kostel',
          'bibliography' => 'some books',
          'description' => 'some desc'
        ]);
    }

    public function testUpdateMarkerWithoutAuth()
    {
        $this->post('/markers/1');
        $this->seeStatusCode(401);
        $this->seeJson([
          'error' => 'Token not provided.'
        ]);
    }
}
