<?php

namespace Tests\Feature;

use App\Models\Image;
use App\Models\Memo;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class ImagesTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        Storage::fake('public');
    }

    /** @test */
    public function images_can_be_uploaded()
    {
        $this->withoutExceptionHandling();
        $this->actingAs($user= factory(User::class)->create(), 'api');
        $file = UploadedFile::fake()->image('test-image.jpg');

        $response = $this->post('/api/images/users/'.$user->id, [
            'image' => $file,
            'width' => '850',
            'height' => '300',
            'location' => 'cover',
        ])->assertStatus(201);

        Storage::disk('public')->assertExists('images/'.$file->hashName());
        $image = Image::first();
        $this->assertEquals('images/'.$file->hashName(), $image->path);
        $this->assertEquals(850, $image->width);
        $this->assertEquals(300, $image->height);
        $this->assertEquals('cover', $image->location);
        $response->assertJson([
            'data' => [
                'type' => 'images',
                'image_id' => $image->id,
                'attributes' => [
                    'path' => url('storage/'.$image->path),
                    'width' => $image->width,
                    'height' => $image->height,
                    'location' => $image->location,

                ]
            ],
            'links' => [
                'self' => url('/images/'.$image->id),
            ]
        ]);
    }

    /** @test */
    public function users_are_returned_with_their_images()
    {
        $this->withoutExceptionHandling();
        $this->actingAs($user= factory(User::class)->create(), 'api');
        $file = UploadedFile::fake()->image('user-image.jpg');

        $this->post('/api/images/users/'.$user->id, [
            'image' => $file,
            'width' => 850,
            'height' => 300,
            'location' => 'cover',
        ])->assertStatus(201);
        $this->post('/api/images/users/'.$user->id, [
            'image' => $file,
            'width' => 850,
            'height' => 300,
            'location' => 'profile',
        ])->assertStatus(201);

        $response = $this->get('/api/users/'.$user->id);

        $userImage = Image::first();
        $response->assertJson([
            'data' => [
                'type' => 'users',
                'user_id' => $user->id,
                'attributes' => [
                    'name' => $user->name,
                    'cover_image' => [
                        'data' => [
                            'type' => 'images',
                            'image_id' => 1,
                            'attributes' => []
                        ]
                    ],
                    'profile_image' => [
                        'data' => [
                            'type' => 'images',
                            'image_id' => 2,
                            'attributes' => []
                        ]
                    ]
                ]
            ]
        ]);
    }

    /** @test */
    public function memos_are_returned_with_their_image_cover()
    {
        $this->withoutExceptionHandling();
        $this->actingAs($user= factory(User::class)->create(), 'api');
        $file = UploadedFile::fake()->image('user-image.jpg');
        $memo = factory(Memo::class)->create(['user_id' => $user->id]);

        $memo->coverImage()->create([
            'path' => $file,
            'width' => 850,
            'height' => 300,
            'location' => 'cover',
        ]);

        $response = $this->get('/api/memos/'.$memo->id);

        $memo = Memo::first();
        $response->assertJson([
            'data' => [
                'type' => 'memos',
                'memo_id' => $memo->id,
                'name' => $memo->name,
                'memo' => $memo->memo,
                'last_updated' => $memo->updated_at->diffForHumans(),
                'attributes' => [
                    'cover_image' => [
                        'data' => [
                            'type' => 'images',
                            'image_id' => 1,
                            'attributes' => []
                        ]
                    ],
                ]
            ],
        ]);
    }

    /** @test */
    public function memos_images_cover_can_be_uploaded()
    {
        $this->withoutExceptionHandling();
        $this->actingAs($user= factory(User::class)->create(), 'api');
        $file = UploadedFile::fake()->image('user-image.jpg');
        $memo = factory(Memo::class)->create(['user_id' => $user->id, 'id' => 123]);

        $this->post('/api/images/memos/'.$memo->id, [
            'image' => $file,
            'width' => 850,
            'height' => 300,
            'location' => 'cover',
        ])->assertStatus(201);

        $image = Image::first();
        $response = $this->get('/api/memos/123');

        $response->assertJson([
            'data' => [
                'type' => 'memos',
                'attributes' => [
                    'cover_image' => [
                        'data' => [
                            'type' => 'images',
                            'image_id' => $image->id,
                            'attributes' => [
                                'path' => url('storage/'.$image->path),
                                'width' => $image->width,
                                'height' => $image->height,
                                'location' => $image->location,

                            ]
                        ],
                        'links' => [
                            'self' => url('/images/'.$memo->coverImage->id),
                        ]
                    ]
                ]
            ]
        ]);
    }
}
