<?php

/**
 * @file
 * Provides hook implementations for the AI Chatbot module.
 */

use Drupal\ai_chatbot\Hook\ChatbotHooks;
use Drupal\Core\Hook\Attribute\LegacyHook;

/**
 * Implements hook_theme().
 */
function ai_chatbot_theme(): array {
  $module_path = \Drupal::service('module_handler')->getModule('ai_chatbot')->getPath();
  return [
    'ai_chatbot' => [
      'variables' => [
        'header' => '',
        'rendered_form' => '',
        'messages' => [],
      ],
    ],
    'ai_chatbot_message' => [
      'variables' => [
        'message' => [],
        'username' => 'Chatbot',
        'role' => 'assistant',
        'timestamp' => '',
        'bot_image' => '',
        'user' => '',
        'assistant_id' => NULL,
        'thread_id' => NULL,
      ],
    ],
    'ai_deepchat' => [
      'variables' => [
        'settings' => [
          'width' => '400px',
          'height' => '500px',
        ],
        'deepchat_settings' => [],
        'current_theme' => '',
      ],
    ],
    'ai_deepchat__toolbar' => [
      'variables' => [
        'settings' => [
          'width' => '400px',
          'height' => '500px',
        ],
        'deepchat_settings' => [],
        'current_theme' => '',
      ],
    ],
    'block__ai_deepchat_block' => [
      'template' => 'block--ai-deepchat-block',
      'path' => $module_path . '/templates/block',
      'base hook' => 'block',
    ],
  ];
}

/**
 * Implements hook_page_attachments().
 */
function ai_chatbot_page_attachments(array &$attachments) {
  $theme = \Drupal::theme()->getActiveTheme()->getName();
  $attachments['#attached']['drupalSettings']['theme'] = $theme;
}

/**
 * Implements hook_form_FORM_ID_alter() for the block form.
 */
function ai_chatbot_form_block_form_alter(&$form, &$form_state) {
  $build_info = $form_state->getBuildInfo();
  if ($build_info['callback_object']->getEntity()->getPluginId() === 'ai_deepchat_block') {
    $form['#attached']['library'][] = 'ai_chatbot/ai_chatbot';
  }

}

/**
 * Implements hook_preprocess_HOOK() for top_bar templates.
 */
#[LegacyHook]
function ai_chatbot_preprocess_top_bar(&$variables) {
  \Drupal::service(ChatbotHooks::class)->topbar($variables);
}

/**
 * Implements hook_toolbar().
 */
#[LegacyHook]
function ai_chatbot_toolbar() {
  return \Drupal::service(ChatbotHooks::class)->toolbar();
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
#[LegacyHook]
function ai_chatbot_theme_suggestions_ai_deepchat_alter(array &$suggestions, array $variables) {
  return \Drupal::service(ChatbotHooks::class)->themeSuggestionsAiDeepchatAlter($suggestions, $variables);
}
