# AI Search Module

Integrates OpenAI ChatGPT with Drupal Search API to provide AI-powered content search with intelligent responses.

## Features

- 🤖 AI-powered search responses using OpenAI GPT-4
- 🔍 Integration with Drupal Search API
- 🎨 Modern, responsive UI with 2025 design trends
- 🌓 Automatic dark mode support
- ⚙️ Customizable styling via override CSS file
- 📱 Mobile-friendly interface

## Help & Documentation

- Drupal Help page: `/admin/help/airagsearch` (also available as a tab on the AI Search settings page)
- Full HTML manual: `modules/custom/airagsearch/help.html` (open directly in a browser or reference via the help page)
- Key admin paths:
  - Settings: `/admin/config/search/airagsearch`
  - Analytics: `/admin/reports/airagsearch-analytics`
  - Search UI: `/airagsearch`
  - Summary: `/airagsearch/summary`

## Installation

1. Place this module in your `/modules/custom/` directory
2. Enable the module: `drush en airagsearch`
3. Clear cache: `drush cr`

## Configuration

### 1. Configure AI Search Settings

Navigate to: **Configuration → Search and metadata → AI Search**  
(`/admin/config/search/airagsearch`)

- **Search API Index**: Select which Search API index to use for searches
- **OpenAI API Key**: Enter your OpenAI API key

### 2. Get OpenAI API Key

1. Visit [OpenAI Platform](https://platform.openai.com/)
2. Sign up or log in
3. Go to API keys section
4. Create a new API key
5. Copy and paste into the module settings

### 3. Configure Search API

Ensure you have a Search API index configured with:
- **Required fields**: `title`, `body`
- Index must be enabled and have indexed content

## Usage

### Using the Form Page

Navigate to `/airagsearch` to access the search form directly.

### Using the Block

1. Go to **Structure → Block layout** (`/admin/structure/block`)
2. Click "Place block" in your desired region
3. Find "AI Search Form" and place it
4. Configure block settings and save

### Search Options

- **All of these words**: Returns results matching ALL search terms (AND logic)
- **Any of these words**: Returns results matching ANY search terms (OR logic)

## Customizing Styles

### Flexible Search Form Layout

The search form uses a seamless flexbox layout where the search input, submit button (icon), and reset button (icon) are visually connected with no gaps. The layout is achieved through:

- **Border management**: Middle element has no left/right borders to avoid doubles
- **Selective border-radius**: Only outer edges are rounded (left on input, right on reset)
- **CSS Custom Properties**: Easy theming without touching the base CSS

#### CSS Custom Properties for Theming

Override these in your theme's CSS to customize the search form:

```css
.ai-search-form-wrapper {
  --search-border-radius: 8px;      /* Outer edge rounding (default: 4px) */
  --search-border-width: 2px;       /* Border thickness (default: 1px) */
  --search-border-color: #2c5aa0;   /* Border color (default: #ddd) */
  --search-max-width: 800px;        /* Max form width (default: 600px) */
  --search-input-height: 50px;      /* Element height (default: 44px, 48px mobile) */
}
```

#### Framework Integration Examples

**Bootstrap Integration:**
```php
$form['search_container']['#attributes']['class'][] = 'input-group';
```

**Tailwind Integration:**
```php
$form['search_container']['#attributes']['class'][] = 'flex';
$form['search_container']['#attributes']['class'][] = 'max-w-2xl';
```

The form works standalone without any framework and adapts to Bootstrap or Tailwind classes when added.

### Method 1: Using the Override File (Recommended)

Edit `/modules/custom/airagsearch/css/ai-search-custom.css` to add your custom styles:

```css
/* Example: Change AI response colors */
.ai-response {
  background: linear-gradient(135deg, #your-color-1 0%, #your-color-2 100%);
}

/* Example: Change link colors */
.result-title {
  color: #your-brand-color;
}

/* Example: Adjust spacing */
.ai-search-results-wrapper {
  margin-top: 3rem;
}
```

After editing, clear cache: `drush cr`

### Method 2: Theme Override

Copy CSS files to your theme and modify:

```
your_theme/
  css/
    ai-search-override.css
```

Add to your theme's `*.libraries.yml`:

```yaml
ai-search-override:
  css:
    theme:
      css/ai-search-override.css: {}
```

## File Structure

```
airagsearch/
├── airagsearch.info.yml
├── airagsearch.libraries.yml
├── airagsearch.module (or airagsearch.theme)
├── airagsearch.permissions.yml
├── airagsearch.routing.yml
├── airagsearch.services.yml
├── airagsearch.links.menu.yml
├── README.md
├── css/
│   ├── ai-search.css
│   └── ai-search-custom.css
├── src/
│   ├── Controller/
│   │   └── AISearchController.php
│   ├── Form/
│   │   ├── AISearchForm.php
│   │   └── AISearchSettingsForm.php
│   ├── Plugin/
│   │   └── Block/
│   │       └── AISearchBlock.php
│   └── Service/
│       └── OpenAIClient.php
└── templates/
    └── ai-search-results.html.twig
```

## Permissions

- **Administer AI Search settings**: Required to configure API key and Search API index
- **Access content**: Required to use the search functionality

## Troubleshooting

### No results showing

1. Verify OpenAI API key is set correctly
2. Check Search API index has indexed content
3. View logs: `drush watchdog:show --type=airagsearch`
4. Clear cache: `drush cr`

### API Errors

- Check your OpenAI API key is valid
- Verify you have API credits available
- Check network connectivity
- Review Drupal logs for detailed error messages

### Styling Issues

1. Clear Drupal cache: `drush cr`
2. Clear browser cache
3. Check that CSS files exist in `/css/` directory
4. Verify library is attached (inspect page source)

## Requirements

- Drupal 10 or 11
- Search API module
- An OpenAI API account with available credits
- PHP 8.1 or higher
- Guzzle HTTP client (included with Drupal)

## Support

For issues and feature requests, please contact your site administrator.

## Credits

Developed for modern Drupal 10/11 applications with AI integration.

For extended guidance consult the HTML manual or visit the Drupal Help page: `/admin/help/airagsearch`.

## License

GPL-2.0-or-later