<?php

namespace App\Tests\Application\Organization;

use App\Tests\Application\OtWebTestCase;

class SubdomainTest extends OtWebTestCase
{
    public function testSubdomainResource(): void
    {
        $this->logger->info("\033[1;34mStarting test... Retrieve the list of subdomain as an admin user\033[0m");
        $this->loginAs($this->user);
        $this->assertResponseIsSuccessful();
        $this->get('/api/subdomains');
        $this->assertResponseStatusCodeSame(200);
        $this->assertJsonContains([
            '@context' => '/api/contexts/Subdomain',
            '@id' => '/api/subdomains',
            '@type' => 'hydra:Collection',
            'hydra:totalItems' => 1,
            'hydra:member' => [
                ['subdomain' => 'subdomain'],
            ],
        ]);
        $this->logger->info("\033[1;32mTest 'test get subdomain collection' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... get all subdomains as a non-admin\033[0m");
        $this->loginAsStudent($this->user);
        $this->get('/api/subdomains');
        $this->assertResponseStatusCodeSame(403);
        $this->logger->info("\033[1;32mTest 'get all subdomains as a non-admin' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... put subdomain as an admin\033[0m");
        $this->loginAs($this->user);
        $this->assertResponseIsSuccessful();
        $this->put('/api/subdomains/1', [
            'subdomain' => 'toto',
            'active' => false,
        ]);
        $this->assertResponseStatusCodeSame(500);
        $this->assertJsonContains([
            '@context' => '/api/contexts/Error',
            '@type' => 'hydra:Error',
            'hydra:title' => 'An error occurred',
            'hydra:description' => 'not supported',
        ]);
        $this->logger->info("\033[1;32mTest 'put subdomain as an admin' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... put subdomain as an non-admin\033[0m");
        $this->loginAs($this->user);
        $this->assertResponseIsSuccessful();
        $this->put('/api/subdomains/1', [
            'subdomain' => 'toto',
            'active' => true,
        ]);
        $this->assertResponseStatusCodeSame(500);
        $this->assertJsonContains([
            '@context' => '/api/contexts/Error',
            '@type' => 'hydra:Error',
            'hydra:title' => 'An error occurred',
            'hydra:description' => 'The subdomain is already active',
        ]);
        $this->logger->info("\033[1;32mTest 'put subdomain as an non-admin' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... get a subdomain as an admin\033[0m");
        $this->loginAs($this->user);
        $this->assertResponseIsSuccessful();
        $this->delete('/api/subdomains/1');
        $this->assertResponseStatusCodeSame(405);
        $this->logger->info("\033[1;32mTest 'get a subdomain as an admin' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... put a subdomain as an non-admin\033[0m");
        $this->loginAsStudent($this->user);
        $this->assertResponseIsSuccessful();
        $this->put('/api/subdomains/1', [
            'subdomain' => 'toto',
            'active' => false,
        ]);
        $this->assertResponseStatusCodeSame(403);
        $this->assertJsonContains([
            '@context' => '/api/contexts/Error',
            '@type' => 'hydra:Error',
            'hydra:title' => 'An error occurred',
        ]);
        $this->logger->info("\033[1;32mTest 'put a subdomains as an non-admin' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... delete a subdomain as an admin\033[0m");
        $this->loginAs($this->user);
        $this->assertResponseIsSuccessful();
        $this->delete('/api/subdomains/1');
        $this->assertResponseStatusCodeSame(405);
        $this->assertJsonContains([
            '@context' => '/api/contexts/Error',
            '@type' => 'hydra:Error',
            'hydra:title' => 'An error occurred',
        ]);
        $this->logger->info("\033[1;32mTest 'delete as an non-admin' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... delete a subdomain as an non-admin\033[0m");
        $this->loginAsStudent($this->user);
        $this->assertResponseIsSuccessful();
        $this->delete('/api/subdomains/1');
        $this->assertResponseStatusCodeSame(405);
        $this->assertJsonContains([
            '@context' => '/api/contexts/Error',
            '@type' => 'hydra:Error',
            'hydra:title' => 'An error occurred',
        ]);
        $this->logger->info("\033[1;32mTest 'delete as an non-admin' succeed\033[0m");
    }
}
