<?php

namespace Drupal\Tests\ai\Functional;

use Drupal\Tests\BrowserTestBase;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Contains AI Settings Form functional tests.
 *
 * @group ai_functional
 */
#[RunTestsInSeparateProcesses]
class AiSettingsFormTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'ai',
    'file',
    'ai_test',
  ];

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

  /**
   * A user with permission to bypass access content.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->adminUser = $this->drupalCreateUser([
      'access administration pages',
      'administer ai',
    ]);
  }

  /**
   * Set up server and index.
   *
   * This both tests the setup and provides a default setup for other tests.
   */
  public function testSettingsFormIsSubmittable(): void {
    $this->drupalLogin($this->adminUser);

    // Set the embedding default provider as the test MySQL one.
    $this->drupalGet('admin/config/ai/settings');
    $this->submitForm([
      'operation__chat' => 'echoai',
    ], 'Choose Model');
    $this->submitForm([
      'operation__chat' => 'echoai',
      'model__chat' => 'gpt-test',
    ], 'Save configuration');

    $this->assertSession()->pageTextContains('The configuration options have been saved.');
    $chat_settings = $this->config('ai.settings')->get('default_providers');
    $this->assertSame([
      'chat' => [
        'provider_id' => 'echoai',
        'model_id' => 'gpt-test',
      ],
    ], $chat_settings);
  }

}
