<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

use Illuminate\Support\Facades\Storage;
use Illuminate\Http\UploadedFile;

use Tests\Feature\ContentElementsTestTrait;

use App\Models\InquiryForm;
use App\Models\User;
use App\Models\Page;
use App\Models\Tag;
use App\Models\FileUpload;
use App\Models\Photo;
use App\Models\ContentElement;

class InquiryFormTest extends TestCase
{
    use ContentElementsTestTrait;

    protected function getClassname()
    {
        return 'inquiry-form';
    }

    public function test_an_inquiry_form_can_be_created()
    {
        $tag = Tag::factory()->create();

        $input = $this->createContentElement(InquiryForm::factory()->notification_email())->toArray();
        $input['id'] = 0;
        $input['content_id'] = 0;
        $input['content_type'] = null;
        $input['content.id'] = 0;
        $input['content']['tags'] = [
            $tag,
        ];

        $page = Page::factory()->create();
        $input['pivot'] = $this->getContentableArray($page, []);
        /*
            'contentable_id' => $page->id,
            'contentable_type' => get_class($page),
            'sort_order' => 1,
            'unlisted' => false,
            'expandable' => false,
            'guest' => false,
        ];
         */

        $this->json('POST', route('content-elements.store'), [])
            ->assertStatus(401);

        $this->signIn(User::factory()->create());

        $this->json('POST', route('content-elements.store'), [])
             ->assertStatus(422)
             ->assertJsonValidationErrors([
                 'pivot.contentable_id',
                 'pivot.contentable_type',
             ]);

        $this->json('POST', route('content-elements.store'), ['pivot' => ['contentable_id' => $page->id, 'contentable_type' => 'page']])
             ->assertStatus(403);

        $this->signInAdmin();

        $this->json('POST', route('content-elements.store'), ['pivot' => ['contentable_id' => $page->id, 'contentable_type' => 'page']])
             ->assertStatus(422)
             ->assertJsonValidationErrors([
                 'type',
             ]);

        $this->json('POST', route('content-elements.store'), ['type' => 'inquiry-form', 'pivot' => ['contentable_id' => $page->id, 'contentable_type' => 'page']])
             ->assertStatus(422)
             ->assertJsonValidationErrors([
                'pivot.sort_order',
                'pivot.unlisted',
                //'pivot.expandable',
                'pivot.guest' => false,
                'pivot.no_margin' => false,
                'pivot.randomize' => false,
             ]);

        $content = $input['content'];
        $input['content'] = [];

        $this->json('POST', route('content-elements.store'), $input)
             ->assertStatus(422)
             ->assertJsonValidationErrors([
                'content.show_student_info',
                'content.show_interests',
                'content.show_livestreams',
                'content.show_livestreams_first',
                'content.create_password',
             ]);

        $input['content'] = $content;

        $this->withoutExceptionHandling();
        $this->json('POST', route('content-elements.store'), $input)
             ->assertSuccessful()
             ->assertJsonFragment([
                'success' => 'Inquiry Form Saved',
             ]);

        $inquiry_form = InquiryForm::all()->last();
        $this->assertEquals(Arr::get($input, 'content.header'), $inquiry_form->header);
        $this->assertEquals(Arr::get($input, 'content.body'), $inquiry_form->body);
        $this->assertEquals(Arr::get($input, 'content.show_student_info'), $inquiry_form->show_student_info);
        $this->assertEquals(Arr::get($input, 'content.show_interests'), $inquiry_form->show_interests);
        $this->assertEquals(Arr::get($input, 'content.show_livestreams'), $inquiry_form->show_livestreams);
        $this->assertEquals(Arr::get($input, 'content.show_livestreams_first'), $inquiry_form->show_livestreams_first);
        $this->assertEquals(Arr::get($input, 'content.show_question'), $inquiry_form->show_question);
        $this->assertEquals(Arr::get($input, 'content.create_password'), $inquiry_form->create_password);
        $this->assertEquals(Arr::get($input, 'content.show_type'), $inquiry_form->show_type);

        $this->assertNotNull($inquiry_form->notification_email);
        $this->assertEquals(Arr::get($input, 'content.notification_email'), $inquiry_form->notification_email);
        $this->assertTrue($inquiry_form->tags->contains('id', $tag->id));
    }


    public function test_an_inquiry_form_can_save_a_photo()
    {
        Storage::fake();
        $file_name = Str::random().'.jpg';
        $file = UploadedFile::fake()->image($file_name);
        $file_upload = (new FileUpload())->saveFile($file, 'photos', true);

        $photo_input = Photo::factory()->raw();
        $photo_input['file_upload_id'] = $file_upload->id;

        $input = $this->createContentElement(InquiryForm::factory())->toArray();
        $input['type'] = 'inquiry-form';
        $input['content'] = InquiryForm::factory()->raw();
        $input['content']['photos'] = [$photo_input];
        $page = Page::factory()->create();
        $input['pivot'] = $this->getContentableArray($page);
        /*
            'contentable_id' => $page->id,
            'contentable_type' => get_class($page),
            'sort_order' => 1,
            'unlisted' => false,
            'expandable' => false,
            'guest' => false,
        ];
         */

        $this->signInAdmin();

        $this->withoutExceptionHandling();
        $this->json('POST', route('content-elements.store'), $input)
             ->assertSuccessful()
             ->assertJsonFragment([
                'success' => 'Inquiry Form Saved',
             ]);

        $content_element = ContentElement::all()->last();

        $inquiry_form = $content_element->content;

        $this->assertEquals(1, $inquiry_form->photos->count());

        $photo = $inquiry_form->photos->first();
        $this->assertInstanceOf(Photo::class, $photo);
        $this->assertEquals(Arr::get($photo_input, 'name'), $photo->name);
        $this->assertEquals(Arr::get($photo_input, 'description'), $photo->description);
        $this->assertEquals(Arr::get($photo_input, 'alt'), $photo->alt);
        $this->assertEquals($photo->fileUpload->id, $file_upload->id);
    }
}
