<?php

namespace Tests\Feature;

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

class SiteUserControllerTest extends TestCase
{
    use RefreshDatabase;
    use UserForTenant;

    protected $tenancy = true;

    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function testShow()
    {
        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->getJson(route('tenant.settings.site-users.show'));

        $response->assertStatus(200);
        $this->assertEquals(2, $response->json('data.limit'));
        $this->assertIsArray($response->json('data.users'));
        $this->assertCount(1, $response->json('data.users'));
    }

    public function testStore()
    {
        Notification::fake();
        $this->seed(GlobalRoleSeed::class);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->postJson(route('tenant.settings.site-users.store'), [
                'email' => 'jdoe@example.com',
                'role' => 'Administrator',
            ]);

        $response->assertStatus(200);

        $user = User::query()->where('email', 'jdoe@example.com')->first();
        $this->assertInstanceOf(User::class, $user);
        $this->assertEquals(1, (new SiteManager())->getSiteUserCount(tenant('id')));
        Notification::assertSentTo($user, AddedToSiteNotification::class);
    }

    public function testUpdate()
    {
        $user = User::factory()->create();
        $user->tenants()->attach(tenant('id'));

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->putJson(route('tenant.settings.site-users.update'), [
                'userId' => $user->id,
                'role' => 'Editor'
            ]);

        $response->assertStatus(200);
    }

    public function testDestroy()
    {
        $user = User::factory()->create();
        $user->tenants()->attach(tenant('id'));

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->deleteJson(route('tenant.settings.site-users.destroy'), [
                'userId' => $user->id,
            ]);

        $response->assertStatus(200);
    }

    public function testFailsUserCreationWhenExceedsUserLimit()
    {
        Notification::fake();
        $tenant = tenant();
        Tenant::query()->where('id', $tenant->id)->update([
            'tier' => Tier::LITE->value,
        ]);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->postJson(route('tenant.settings.site-users.store'), [
                'email' => 'jdoe@example.com',
                'role' => 'Administrator',
            ]);
        $response->assertStatus(200);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->postJson(route('tenant.settings.site-users.store'), [
                'email' => 'jdoe2@example.com',
                'role' => 'Administrator',
            ]);
        $response->assertStatus(200);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->postJson(route('tenant.settings.site-users.store'), [
                'email' => 'jdoe3@example.com',
                'role' => 'Administrator',
            ]);
        $response->assertStatus(403);
    }
}
