<?php

declare(strict_types=1);

namespace Drupal\Tests\acquia_dam\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\user\UserInterface;

/**
 * Tests the /user/{uid}/acquia-dam page authorization flow.
 *
 * @group acquia_dam
 */
class AcquiaDamUserAuthorizationButtonTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['acquia_dam', 'media', 'media_library', 'user'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Tests the Authorize → log out link lifecycle on the /user/{uid}/acquia-dam page.
   */
  public function testUserDamAuthorizationPage(): void {
    $this->grantDamDomain();
    $this->grantSiteRegistrationToken();
    $account = $this->drupalCreateUser([], NULL, TRUE);
    $this->drupalLogin($account);

    // Visit the user profile and click the "Acquia DAM" primary tab.
    $this->drupalGet('/user/' . $account->id() . '/acquia-dam');
    $this->assertSession()->statusCodeEquals(200);
    // Assert: page description is present.
    $this->assertSession()->pageTextContains('Connect to Acquia DAM');
    // Assert: "Authorize" link with primary-button CSS classes.
    $this->assertSession()->linkExists('Authorize');
    $this->assertSession()->elementExists(
      'css',
      'a.acquia-dam-button.acquia-dam-button--primary.button.button--primary'
    );

    // Complete the DAM OAuth flow for the current user.
    $this->grantCurrentUserDamToken($account);
    // Revisit /user/{uid}/acquia-dam after DAM authentication.
    $this->drupalGet('/user/' . $account->id() . '/acquia-dam');
    $this->assertSession()->statusCodeEquals(200);
    // Assert: "log out" link with primary-button CSS classes.
    $this->assertSession()->linkExists('Log out');
    $this->assertSession()->elementExists(
      'css',
      'a.acquia-dam-button.acquia-dam-button--danger.button.button--danger'
    );
  }

  /**
   * Grants the site a dummy DAM domain.
   */
  protected function grantDamDomain() {
    /** @var \Drupal\Core\Config\ConfigFactoryInterface $configFactory */
    $configFactory = $this->container->get('config.factory');
    $configFactory->getEditable('acquia_dam.settings')
      ->set('domain', 'test.widencollective.com')
      ->save();
  }

  /**
   * Grants the site a dummy DAM token and domain configuration.
   */
  protected function grantSiteRegistrationToken() {
    $this->grantDamDomain();
    $this->container->get('state')->set(
      'acquia_dam_token',
      $this->randomString()
    );
  }

  /**
   * Grants a DAM token to simulate a completed DAM OAuth flow.
   *
   * @param \Drupal\user\UserInterface $account
   *   The account to authenticate with DAM.
   * @param string|null $token
   *   The DAM access token to store; a random string is used when NULL.
   */
  protected function grantCurrentUserDamToken(UserInterface $account, ?string $token = NULL): void {
    if (!$token) {
      $token = $this->randomString();
    }
    $this->container->get('acquia_dam.authentication_service')->setUserData(
      (int) $account->id(),
      [
        'acquia_dam_username' => $account->getEmail(),
        'acquia_dam_token' => $token,
      ]
    );
  }

}
