<?php
/**
 * File: app/Admin/config/aiServices.php.
 *
 * @package KimaAI
 * @since 1.1.0
 */

use KimaAI\Helper;

$kimaai_reasoning_model_ids = array_merge(
  Helper::aiModelIdsWithParam('openai', 'reasoning'),
  Helper::aiModelIdsWithParam('openrouter', 'reasoning'),
  Helper::aiModelIdsWithParam('replicate', 'reasoning'),
  Helper::aiModelIdsWithParam('ollama', 'reasoning'),
);

$kimaai_non_temperature_model_ids = array_values(array_unique(array_merge(
  $kimaai_reasoning_model_ids,
  Helper::aiModelIdsWithParam('openai', 'verbosity'),
  Helper::aiModelIdsWithParam('openrouter', 'verbosity'),
)));

return [
  'order' => 1,
  'schema' => [
    'type' => 'object',
    'title' => __('AI Services', 'kimaai'),
    'icon' => 'CpuChip',
    'isOpen' => true,
    'properties' => [
      'aiEnvs' => [
        'type' => 'array',
        'default' => [],
        'saveType' => 'repeatable',
        'title' => __('AI Environments', 'kimaai'),
        'link' => 'ai-envs',
        'pageTitle' => __('AI Environments', 'kimaai'),
        'pageDescription' => __('Here you can set the environment for the different AI services', 'kimaai'),
        'actions' => [
          'test-connection' => '/kimaai/v1/test-connection',
        ],
        'items' => [
          'type' => 'object',
          'default' => [],
          'properties' => [
            'id' => [
              'type' => 'string',
              'default' => '',
            ],
            'name' => [
              'type' => 'string',
              'default' => 'default',
              'required' => true,
              'component' => 'TextField',
              'props' => [
                'type' => 'text',
                'label' => __('Name', 'kimaai'),
              ],
            ],
            'provider' => [
              'type' => 'string',
              'default' => 'openai',
              'required' => true,
              'component' => 'SelectField',
              'props' => [
                'label' => __('Provider', 'kimaai'),
                'options' => [
                  [
                    'value' => 'openai',
                    'label' => 'OpenAI',
                  ],
                  [
                    'value' => 'openrouter',
                    'label' => 'OpenRouter',
                  ],
                  [
                    'value' => 'replicate',
                    'label' => 'Replicate',
                  ],
                  [
                    'value' => 'ollama',
                    'label' => 'Ollama',
                  ],
                ],
              ],
            ],
            'apiKey' => [
              'type' => 'string',
              'default' => '',
              'required' => true,
              'component' => 'TextField',
              'condition' => [
                'field' => 'provider',
                'operator' => '!=',
                'value' => 'ollama',
              ],
              'props' => [
                'type' => 'password',
                'label' => __('API Key', 'kimaai'),
                'description' => __('Put your environment API key here', 'kimaai'),
              ],
            ],
            'endpoint' => [
              'type' => 'string',
              'default' => '',
              'required' => true,
              'component' => 'TextField',
              'condition' => [
                'field' => 'provider',
                'operator' => '=',
                'value' => 'ollama',
              ],
              'props' => [
                'type' => 'text',
                'label' => __('Endpoint', 'kimaai'),
                'description' => __('Put your environment API endpoint here', 'kimaai'),
              ],
            ],
          ],
        ],
      ],
      'aiPresets' => [
        'type' => 'object',
        'title' => __('AI Presets', 'kimaai'),
        'link' => 'ai-presets',
        'pageTitle' => __('AI Presets', 'kimaai'),
        'pageDescription' => __('Here you can set the presets for the different AI services', 'kimaai'),
        'auto' => 'first_sync:aiEnvs',
        'sections' => [
          'default' => [
            'title' => __('Default AI Settings', 'kimaai'),
            'description' => __('These settings will be used as default for all AI requests unless overridden.', 'kimaai'),
          ],
          'chat' => [
            'title' => __('Chat AI Settings', 'kimaai'),
            'description' => __('These settings will be used for chat-based AI requests unless overridden.', 'kimaai'),
          ],
          'image' => [
            'title' => __('Image AI Settings', 'kimaai'),
            'description' => __('These settings will be used for image generation AI requests unless overridden.', 'kimaai'),
          ],
          'embedding' => [
            'title' => __('Embedding AI Settings', 'kimaai'),
            'description' => __('These settings will be used for embedding AI requests unless overridden.', 'kimaai'),
          ],
        ],
        'properties' => [
          'defaultEnv' => [
            'section' => 'default',
            'type' => 'string',
            'default' => 'default',
            'component' => 'SelectField',
            'props' => [
              'label' => __('Default Environment', 'kimaai'),
              'options' => Helper::getAiEnvsOptions(),
              'description' => __('Choose the environment for the Default AI Environment.', 'kimaai'),
            ],
          ],
          'defaultModel' => [
            'section' => 'default',
            'type' => 'string',
            'default' => 'gpt-4o-mini',
            'component' => 'SelectField',
            'props' => [
              'label' => __('Default Model', 'kimaai'),
              'options' => Helper::aiProviderModelOptions('defaultEnv', ['completion']),
              'description' => __('Choose the model for the environment.', 'kimaai'),
            ],
          ],
          'defaultReasoning' => [
            'section' => 'default',
            'type' => 'string',
            'default' => 'medium',
            'component' => 'SelectField',
            'props' => [
              'label' => __('Reasoning Level', 'kimaai'),
              'description' => __('Control how much reasoning effort the model uses.', 'kimaai'),
              'options' => array_merge(
                Helper::aiModelParamOptions('openai', 'reasoning', 'defaultModel'),
                Helper::aiModelParamOptions('openrouter', 'reasoning', 'defaultModel'),
                Helper::aiModelParamOptions('replicate', 'reasoning', 'defaultModel'),
                Helper::aiModelParamOptions('ollama', 'reasoning', 'defaultModel'),
              ),
            ],
            'condition' => [
              'field' => 'defaultModel',
              'operator' => 'in',
              'value' => $kimaai_reasoning_model_ids,
            ],
          ],
          'defaultVerbosity' => [
            'section' => 'default',
            'type' => 'string',
            'default' => 'medium',
            'component' => 'SelectField',
            'props' => [
              'label' => __('Verbosity', 'kimaai'),
              'description' => __('Control how concise or detailed the model is.', 'kimaai'),
              'options' => array_merge(
                Helper::aiModelParamOptions('openai', 'verbosity', 'defaultModel'),
                Helper::aiModelParamOptions('openrouter', 'verbosity', 'defaultModel'),
              ),
            ],
            'condition' => [
              'field' => 'defaultModel',
              'operator' => 'in',
              'value' => array_merge(
                Helper::aiModelIdsWithParam('openai', 'verbosity'),
                Helper::aiModelIdsWithParam('openrouter', 'verbosity'),
              ),
            ],
          ],
          'defaultTemperature' => [
            'section' => 'default',
            'type' => 'number',
            'default' => 0.7,
            'component' => 'RangeField',
            'props' => [
              'initialPosition' => 0.7,
              'min' => 0,
              'max' => 2,
              'step' => 0.1,
              'label' => __('Temperature', 'kimaai'),
              'description' => __('Set the temperature for the AI model. A higher value means more creative responses.', 'kimaai'),
            ],
            'condition' => [
              'field' => 'defaultModel',
              'operator' => 'not_in',
              'value' => $kimaai_non_temperature_model_ids,
            ],
          ],
          'chatEnv' => [
            'section' => 'chat',
            'type' => 'string',
            'default' => 'default',
            'component' => 'SelectField',
            'props' => [
              'label' => __('Chat Environment', 'kimaai'),
              'options' => Helper::getAiEnvsOptions(),
              'description' => __('Choose the environment for the Chat AI Environment.', 'kimaai'),
            ],
          ],
          'chatModel' => [
            'section' => 'chat',
            'type' => 'string',
            'default' => 'gpt-4o-mini',
            'component' => 'SelectField',
            'props' => [
              'label' => __('Chat Model', 'kimaai'),
              'options' => Helper::aiProviderModelOptions('chatEnv', ['completion']),
              'description' => __('Choose the model for the Chat environment.', 'kimaai'),
            ],
          ],
          'chatReasoning' => [
            'section' => 'chat',
            'type' => 'string',
            'default' => 'medium',
            'component' => 'SelectField',
            'props' => [
              'label' => __('Reasoning Level', 'kimaai'),
              'description' => __('Control how much reasoning effort the model uses.', 'kimaai'),
              'options' => array_merge(
                Helper::aiModelParamOptions('openai', 'reasoning', 'chatModel'),
                Helper::aiModelParamOptions('openrouter', 'reasoning', 'chatModel'),
                Helper::aiModelParamOptions('replicate', 'reasoning', 'chatModel'),
                Helper::aiModelParamOptions('ollama', 'reasoning', 'chatModel'),
              ),
            ],
            'condition' => [
              'field' => 'chatModel',
              'operator' => 'in',
              'value' => $kimaai_reasoning_model_ids,
            ],
          ],
          'chatVerbosity' => [
            'section' => 'chat',
            'type' => 'string',
            'default' => 'medium',
            'component' => 'SelectField',
            'props' => [
              'label' => __('Verbosity', 'kimaai'),
              'description' => __('Control how concise or detailed the model is.', 'kimaai'),
              'options' => array_merge(
                Helper::aiModelParamOptions('openai', 'verbosity', 'chatModel'),
                Helper::aiModelParamOptions('openrouter', 'verbosity', 'chatModel'),
              ),
            ],
            'condition' => [
              'field' => 'chatModel',
              'operator' => 'in',
              'value' => array_merge(
                Helper::aiModelIdsWithParam('openai', 'verbosity'),
                Helper::aiModelIdsWithParam('openrouter', 'verbosity'),
              ),
            ],
          ],
          'chatTemperature' => [
            'section' => 'chat',
            'type' => 'number',
            'default' => 0.7,
            'component' => 'RangeField',
            'props' => [
              'initialPosition' => 0.7,
              'min' => 0,
              'max' => 2,
              'step' => 0.1,
              'label' => __('Temperature', 'kimaai'),
              'description' => __('Set the temperature for the AI model. A higher value means more creative responses.', 'kimaai'),
            ],
            'condition' => [
              'field' => 'chatModel',
              'operator' => 'not_in',
              'value' => $kimaai_non_temperature_model_ids,
            ],
          ],
          'imageEnv' => [
            'section' => 'image',
            'type' => 'string',
            'default' => 'default',
            'autoSyncExcludeProviders' => ['openrouter'],
            'component' => 'SelectField',
            'props' => [
              'label' => __('Image Environment', 'kimaai'),
              'options' => Helper::getAiEnvsOptions(true, false, ['openrouter']),
              'description' => __('Choose the environment for the Image AI Environment.', 'kimaai'),
            ],
          ],
          'imageModel' => [
            'section' => 'image',
            'type' => 'string',
            'default' => 'dall-e-2',
            'component' => 'SelectField',
            'props' => [
              'label' => __('Image Model', 'kimaai'),
              'options' => [
                [
                  'value' => 'black-forest-labs/flux-schnell',
                  'label' => __('FLUX Schnell', 'kimaai'),
                  'condition' => [
                    'field' => 'imageEnv',
                    'operator' => 'contains',
                    'value' => 'replicate',
                  ],
                ],
                [
                  'value' => 'black-forest-labs/flux-dev',
                  'label' => __('FLUX Dev', 'kimaai'),
                  'condition' => [
                    'field' => 'imageEnv',
                    'operator' => 'contains',
                    'value' => 'replicate',
                  ],
                ],
                [
                  'value' => 'dall-e-2',
                  'label' => __('DALL-E 2', 'kimaai'),
                  'condition' => [
                    'field' => 'imageEnv',
                    'operator' => 'contains',
                    'value' => 'openai',
                  ],
                ],
                [
                  'value' => 'dall-e-3',
                  'label' => __('DALL-E 3', 'kimaai'),
                  'condition' => [
                    'field' => 'imageEnv',
                    'operator' => 'contains',
                    'value' => 'openai',
                  ],
                ],
                [
                  'value' => 'gpt-image-1',
                  'label' => __('GPT Image 1', 'kimaai'),
                  'condition' => [
                    'field' => 'imageEnv',
                    'operator' => 'contains',
                    'value' => 'openai',
                  ],
                ],
              ],
              'description' => __('Choose the model for the Image environment.', 'kimaai'),
            ],
          ],
          'embedEnv' => [
            'section' => 'embedding',
            'type' => 'string',
            'default' => 'default',
            'component' => 'SelectField',
            'props' => [
              'label' => __('Embedding Environment', 'kimaai'),
              'options' => Helper::getAiEnvsOptions(true),
              'description' => __('Choose the environment for the Embedding AI Environment.', 'kimaai'),
              'onlyPremium' => !Helper::isPremium(),
            ],
          ],
          'embedModel' => [
            'section' => 'embedding',
            'type' => 'string',
            'default' => 'text-embedding-3-small',
            'component' => 'SelectField',
            'props' => [
              'label' => __('Embedding Model', 'kimaai'),
              'options' => Helper::aiProviderModelOptions('embedEnv', ['embedding']),
              'description' => __('Choose the model for the Embedding environment.', 'kimaai'),
              'onlyPremium' => !Helper::isPremium(),
            ],
          ],
        ],
      ],
    ],
  ],
];
