<?php

namespace Tests\Feature;

use App\Notifications\AddedToSiteNotification;
use App\Tier;
use Carbon\Carbon;
use Database\Seeders\Global\GlobalRoleSeed;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Notification;
use Mtc\MercuryDataModels\Colour;
use Mtc\MercuryDataModels\Tenant;
use Mtc\MercuryDataModels\User;
use Mtc\MercuryDataModels\Vehicle;
use Tests\TestCase;
use Tests\UserForTenant;

class ColourMapTest extends TestCase
{
    use RefreshDatabase;
    use UserForTenant;

    protected $tenancy = true;

    public function testIndex()
    {
        $colour = Colour::factory()->create();
        Colour::factory(3)->create();
        Vehicle::factory()->create(['colour' => 'foo']);
        Vehicle::factory(2)->create(['colour' => 'baz']);
        $colour->mappedColours()->create(['sub_colour' => 'foo']);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->getJson(route('tenant.colour-map.index'));

        $response->assertStatus(200);

        $this->assertArrayHasKey('colours', $response->json());
        $this->assertCount(4, $response->json('colours'));
        $this->assertArrayHasKey('id', $response->json('colours.0'));
        $this->assertArrayHasKey('colour', $response->json('colours.0'));
        $this->assertArrayHasKey('hex_code', $response->json('colours.0'));
        $this->assertArrayHasKey('subColours', $response->json('colours.0'));
        $this->assertTrue(collect($response->json('colours'))->where('colour', $colour->colour)->count() > 0);
        $colourResponse = collect($response->json('colours'))->where('id', $colour->id)->first();
        $this->assertTrue(collect($colourResponse['subColours'])->where('name', 'foo')->count() == 1);

        $this->assertArrayHasKey('subColours', $response->json());
        $this->assertCount(1, $response->json('subColours'));
        $this->assertArrayHasKey('name', $response->json('subColours.0'));
    }

    public function testUpdate()
    {
        Colour::factory()->create();
        $colour = Colour::factory()->create();
        Vehicle::factory()->create(['colour' => 'foo']);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->putJson(route('tenant.colour-map.update'), [
                'colours' => [
                    [
                        'id' => $colour->id,
                        'colour' => 'foo baz',
                        'hex_code' => 'ABC123',
                        'subColours' => [
                            ['name' => 'foo'],
                        ],
                    ],
                    [
                        'id' => 'abc12312aa',
                        'colour' => 'lorem ipsum',
                        'hex_code' => null,
                        'subColours' => [],
                    ],
                ]
            ]);

        $response->assertStatus(200);
        $this->assertTrue(collect($response->json('colours'))->where('colour', 'lorem ipsum')->count() > 0);
        $colourResponse = collect($response->json('colours'))->where('id', $colour->id)->first();
        $this->assertEquals('foo baz', $colourResponse['colour']);
        $this->assertTrue(collect($colourResponse['subColours'])->where('name', 'foo')->count() == 1);
    }
}
