<?php

namespace Drupal\Tests\acquia_dam\Kernel;

use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Test config form access.
 *
 * @group acquia_dam_kernel
 */
class AcquiaDamConfigurationFormTest extends AcquiaDamKernelTestBase {

  /**
   * Tests config form access for admin.
   */
  public function testFormAccessAdmin(): void {
    $this->drupalSetUpCurrentUser([], [], TRUE);

    // Configure Acquia DAM.
    $request = $this->getMockedRequest('/admin/config/acquia-dam', 'GET');
    $response = $this->processRequest($request);
    self::assertEquals(200, $response->getStatusCode());

    // Disconnect the site from Acquia DAM.
    $request = $this->getMockedRequest('/acquia-dam/disconnect', 'GET');
    $response = $this->processRequest($request);
    self::assertEquals(200, $response->getStatusCode());
  }

  /**
   * Provides data for testFormAccess.
   *
   * @return array
   */
  public static function formAccessDataProvider(): array {
    return [
      'main config with permission' => ['/admin/config/acquia-dam', TRUE, 200],
      'main config without permission' => ['/admin/config/acquia-dam', FALSE, 403],
      'metadata with permission' => ['/admin/config/acquia-dam/metadata', TRUE, 200],
      'metadata without permission' => ['/admin/config/acquia-dam/metadata', FALSE, 403],
      'image-styles with permission' => ['/admin/config/acquia-dam/image-styles', TRUE, 200],
      'image-styles without permission' => ['/admin/config/acquia-dam/image-styles', FALSE, 403],
      'integration-links with permission' => ['/admin/config/acquia-dam/integration-links', TRUE, 200],
      'integration-links without permission' => ['/admin/config/acquia-dam/integration-links', FALSE, 403],
    ];
  }

  /**
   * Tests config form access for user with permission.
   *
   * @dataProvider formAccessDataProvider
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function testFormAccessWithPermission(string $path, bool $withPermission, int $expectedStatus): void {
    $permissions = $withPermission ? ['administer acquia_dam'] : [];
    $this->drupalSetUpCurrentUser([], $permissions);
    $request = $this->getMockedRequest($path, 'GET');
    $response = $this->processRequest($request);
    self::assertEquals($expectedStatus, $response->getStatusCode());
  }

  /**
   * Tests disconnecting the site and changing the domain.
   *
   * @throws \Exception
   */
  public function testFormDisconnect(): void {
    $this->drupalSetUpCurrentUser([], [
      'administer acquia_dam',
    ]);
    // Step 1: Disconnect the site from Acquia DAM.
    $request = $this->getMockedRequest('/acquia-dam/disconnect', 'GET');
    $response = $this->processRequest($request);
    self::assertEquals(200, $response->getStatusCode());
    self::assertStringContainsString('Are you sure you want to disconnect this site from Acquia DAM?', (string) $response->getContent());
    $response = $this->doFormSubmit(
      '/acquia-dam/disconnect',
      [],
      'Disconnect'
    );
    self::assertEquals(303, $response->getStatusCode());

    // Visit configuration page to verify the success message and disconnection
    $request = $this->getMockedRequest("/admin/config/acquia-dam", 'GET');
    $response = $this->processRequest($request);
    self::assertEquals(200, $response->getStatusCode());
    self::assertStringContainsString('Site successfully disconnected from Acquia DAM.', (string) $response->getContent());

    // Step 2: Change the domain to "https://test2.widencollective.com".
    $data = [
      'access_token' => $this->randomString(),
      'domain' => 'https://test2.widencollective.com',
      'op' => 'Authenticate Site',
    ];

    // Submit the form with the new domain.
    $response = $this->doFormSubmit(
      '/admin/config/acquia-dam',
      $data
    );
    // This should result in a 303 redirect to the authentication URL.
    self::assertEquals(303, $response->getStatusCode());

    // Step 3: Verify the domain is updated in the configuration.
    $config = $this->container->get('config.factory')->get('acquia_dam.settings');
    self::assertEquals('test2.widencollective.com', $config->get('domain'));

    // Step 4: Visit the configuration page and verify the new domain is displayed.
    $request = $this->getMockedRequest("/admin/config/acquia-dam", 'GET');
    $response = $this->processRequest($request);
    self::assertEquals(200, $response->getStatusCode());

    // Check for the domain field containing the new domain value.
    self::assertStringContainsString('test2.widencollective.com', (string) $response->getContent());
  }

}
