# AI-Driven Support Chat Proposal

This document outlines the technical strategy for transforming the current support bubble submission into an interactive AI-first chat experience.

## 1. Architectural Overview
Instead of a "Submit and Forget" ticketing flow, we will implement a "Chat-to-Ticket" flow. 
- **The Entry Point**: The existing Spatie Support Bubble.
- **The Intermediary**: A new database-backed conversation engine.
- **The Intelligence**: Gemini AI via `AiSupportService`.
- **The Fallback**: Manual ticketing in `binshops/laravel-ticket`.

## 2. Core Enhancements

### A. Database-Backed Dialogue
Following the plan, we will utilize the `conversations` and `messages` tables to persist the state. This allows:
- **Contextual Awareness**: Gemini can see the last 5-10 messages to understand the thread.
- **Escalation Tracking**: We know if a user has already tried the AI before opening a ticket.

### B. Controller Logic Transformation
The `SupportChatController` will be evolved from a single-action invokable class to a multi-purpose controller:
- `chat()`: The main entry point. It will check for an existing `conversation_id` in the session or body.
- `escalate()`: A specialized method that creates a Binshops ticket from the conversation history and updates the `escalated` flag.

### C. Integrating Gemini with History
We will update `AiSupportService::reply()` to accept the message history. This prevents the "Who are you again?" problem if the user asks a follow-up question.

**Proposed Signature Update:**
```php
public function reply(string $userMessage, array $history = [])
{
    // Build the "contents" block for Gemini using the history array
    // ...
}
```

## 3. The "Chat in a Bubble" UX Strategy
Since the Spatie Support Bubble is a simple form, we have two paths for the User Experience:

### Path Alpha: The Session-Based Single Reply (Simplest)
1. User provides Name/Email/Message.
2. AI generates a reply.
3. The "Success Message" shown in the bubble *is* the AI reply + a link: "Not helpful? Talk to a human."

### Path Beta: Persistent AJAX Chat (Advanced)
1. Replace the bubble's internal submission with a small piece of custom JavaScript.
2. Submissions go to `/support/chat` via AJAX.
3. The UI updates in real-time to show the AI response without refreshing the bubble form.

## 4. Ticket Escalation Logic
Tickets will be generated automatically under the following conditions:
- **AI Failure**: Gemini outputs a pre-defined "Escalation Flag" (e.g., "I'm connecting you...").
- **Message Threshold**: After 3-4 replies (as proposed in the plan), the system assumes the AI is stuck.
- **Manual Request**: The user selects an "Escalate to Human" option in the UI.

## 5. Implementation Roadmap
1. **Migrations**: Execute the `conversations` and `messages` migrations.
2. **Persistence Layer**: Create the `Conversation` and `Message` models with appropriate relationships.
3. **Service Update**: Enhance `AiSupportService` to handle multi-turn history.
4. **Controller Update**: Shift `SupportChatController` to use the bridge pattern between conversations and tickets.
5. **Route Update**: Register the new endpoints.

---
*End of Proposal. Standing by for instructions.*
