<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/

class Intopenai_Form_Admin_Global extends Engine_Form
{
  public function init()
  {
		$settings = Engine_Api::_()->getApi('settings', 'core');

		$supportTicket = '<a href="https://socialengine.com/store/clientarea.php?action=products&module=licensing" target="_blank">click here</a>';
		$descriptionLicense = sprintf('Enter your license key that is provided to you when you purchased this plugin. If you do not know your license key, please %s. (Key Format: SEPHP-XXXXXXXXXXXXXXXX)',$supportTicket);

		$this->addElement('Text', 'intopenai_licensekey', array(
			'label' => 'License Key',
			'description' => $descriptionLicense,
			'required' => true,
			'allowEmpty' => false,
			'validators' => array(
				array('NotEmpty', true),
			),
			'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('intopenai.licensekey'),
		));
		$this->getElement('intopenai_licensekey')->getDecorator('Description')->setOptions(array('placement' => 'PREPEND', 'escape' => false));

		if (engine_in_array($_SERVER['SERVER_NAME'], ['localhost', '127.0.0.1'])) {
			$this->addElement('Text', 'domain_name', array(
				'label' => 'Domain Name',
				'required' => true,
				'allowEmpty' => false,
				'validators' => array(
					array('NotEmpty', true),
				),
				'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('domain.name'),
			));
		}

		if (Engine_Api::_()->getApi('settings', 'core')->getSetting('intopenai.pluginactivated')) {

			$description = $this->getTranslator()->translate(
				'You can create the OpenAI secret key from');

			if( $settings->getSetting('user.support.links', 0) == 1 ) {
				$moreinfo = $this->getTranslator()->translate(
						'<a href="%1$s" target="_blank"> here</a>.');
			} else {
				$moreinfo = $this->getTranslator()->translate(
						'');
			}

      $description = vsprintf($description.$moreinfo, array(
        'https://platform.openai.com/',
      ));

    	// Decorators
      $this->loadDefaultDecorators();
    	$this->getDecorator('Description')->setOption('escape', false);

			$this
				->setTitle('Global Settings');
				$this->setDescription($description);

			$this->addElement('Text', 'intopenai_key', array(
				'label' => 'OpenAI Secret Key',
				'required' => true,
				'allowEmpty' => false,
				'description' => '', 
				'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('intopenai_key'),
			));

			if (_ENGINE_ADMIN_NEUTER) {
				$this->intopenai_key->setValue('*********');
			} 

			$this->addElement('Select', 'intopenai_model', array(
					'label' => 'OpenAI Model',
					'description' => '',
					'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('intopenai_model','gpt-4'),
					'multiOptions' => array(
							'gpt-4' => 'gpt-4',
							'gpt-3.5-turbo' => 'gpt-3.5-turbo',
					),
			));
			
			// Add submit button
			$this->addElement('Button', 'submit', array(
				'label' => 'Save Changes',
				'type' => 'submit',
				'ignore' => true
			));
		} else {
			//Add submit button
      $this->addElement('Button', 'submit', array(
          'label' => 'Activate your plugin',
          'type' => 'submit',
          'ignore' => true
      ));
		}
  }
}
