# Slidev Generation API

## Overview
The Slidev Generation API allows you to generate presentation decks from templates with custom data.

## Endpoint

```
POST /api/slidev/generate
```

## Authentication
Currently, no authentication is required. The endpoint validates that the session exists in the database.

## Rate Limiting
- **Limit**: 10 requests per minute per session
- **Window**: 60 seconds
- **Key**: Session ID from request body (falls back to IP address)
- **Response on limit exceeded**: 429 Too Many Requests

When rate limited, the response will include standard `RateLimit-*` headers:
- `RateLimit-Limit`: Maximum number of requests allowed
- `RateLimit-Remaining`: Number of requests remaining
- `RateLimit-Reset`: Time when the rate limit resets (Unix timestamp)

## Request Format

### Headers
```
Content-Type: application/json
```

### Request Body

```typescript
interface GenerateRequest {
  sessionId: string;      // UUID of the conversation session
  templateId: string;     // Template identifier (currently only 'starter' supported)
  data: Record<string, any>;  // Key-value pairs for placeholder replacement
}
```

### Example Request

```bash
curl -X POST http://localhost:3000/api/slidev/generate \
  -H "Content-Type: application/json" \
  -d '{
    "sessionId": "550e8400-e29b-41d4-a716-446655440000",
    "templateId": "starter",
    "data": {
      "title": "My Presentation",
      "subtitle": "An awesome deck",
      "author": "John Doe"
    }
  }'
```

## Response Format

### Success Response (200 OK)

```typescript
interface GenerateResponse {
  deckId: string;        // Unique identifier for the generated deck
  deckUrl: string;       // URL where the deck can be accessed
  message: string;       // Success message
}
```

#### Example Success Response

```json
{
  "deckId": "abc123-20241009-123456",
  "deckUrl": "http://localhost:3001/d/abc123-20241009-123456",
  "message": "Deck generated successfully"
}
```

### Error Responses

#### 400 Bad Request - Validation Failed

Returned when the request body is missing required fields or contains invalid data.

```json
{
  "error": "Validation failed",
  "details": "\"sessionId\" must be a valid GUID"
}
```

**Common validation errors:**
- `sessionId` must be a valid UUID
- `templateId` must be 'starter' (currently the only supported template)
- `data` must be an object

#### 404 Not Found - Session Not Found

Returned when the specified session does not exist in the database.

```json
{
  "error": "Session not found",
  "details": "No session exists with ID: 550e8400-e29b-41d4-a716-446655440000"
}
```

#### 404 Not Found - Template Not Found

Returned when the specified template does not exist.

```json
{
  "error": "Template not found",
  "details": "Template 'invalid-template' does not exist"
}
```

#### 429 Too Many Requests - Rate Limit Exceeded

Returned when you exceed the rate limit of 10 requests per minute.

```json
{
  "message": "Too many generation requests, please try again later"
}
```

Headers will include:
```
RateLimit-Limit: 10
RateLimit-Remaining: 0
RateLimit-Reset: 1696867200
```

#### 500 Internal Server Error - Database Connection Failed

Returned when the API cannot connect to the database.

```json
{
  "error": "Database connection failed",
  "details": "Unable to connect to database"
}
```

#### 500 Internal Server Error - Build Timeout

Returned when deck generation takes too long (exceeds timeout).

```json
{
  "error": "Build timeout",
  "details": "Deck build exceeded maximum allowed time"
}
```

#### 500 Internal Server Error - Build Error

Returned when the deck build process fails.

```json
{
  "error": "Failed to generate deck",
  "details": "Build process failed: <error details>"
}
```

#### 500 Internal Server Error - Generic Error

Returned for other unexpected errors.

```json
{
  "error": "Failed to generate deck",
  "details": "<error message>"
}
```

## Error Code Summary

| Status Code | Error Type | Description |
|-------------|------------|-------------|
| 400 | Validation Failed | Invalid or missing request fields |
| 404 | Session Not Found | Session ID does not exist in database |
| 404 | Template Not Found | Template ID is not recognized |
| 429 | Too Many Requests | Rate limit exceeded |
| 500 | Database Connection Failed | Cannot connect to database |
| 500 | Build Timeout | Generation took too long |
| 500 | Build Error | Deck build process failed |
| 500 | Internal Server Error | Other unexpected errors |

## Example Usage Scenarios

### Successful Generation

```bash
curl -X POST http://localhost:3000/api/slidev/generate \
  -H "Content-Type: application/json" \
  -d '{
    "sessionId": "550e8400-e29b-41d4-a716-446655440000",
    "templateId": "starter",
    "data": {
      "title": "Product Launch 2024",
      "subtitle": "Revolutionary Features",
      "author": "Product Team"
    }
  }'
```

**Response:**
```json
{
  "deckId": "prod-launch-20241009-143022",
  "deckUrl": "http://localhost:3001/d/prod-launch-20241009-143022",
  "message": "Deck generated successfully"
}
```

### Invalid Session ID

```bash
curl -X POST http://localhost:3000/api/slidev/generate \
  -H "Content-Type: application/json" \
  -d '{
    "sessionId": "invalid-id",
    "templateId": "starter",
    "data": {}
  }'
```

**Response (400):**
```json
{
  "error": "Validation failed",
  "details": "\"sessionId\" must be a valid GUID"
}
```

### Session Not Found

```bash
curl -X POST http://localhost:3000/api/slidev/generate \
  -H "Content-Type: application/json" \
  -d '{
    "sessionId": "00000000-0000-0000-0000-000000000000",
    "templateId": "starter",
    "data": {}
  }'
```

**Response (404):**
```json
{
  "error": "Session not found",
  "details": "No session exists with ID: 00000000-0000-0000-0000-000000000000"
}
```

### Rate Limit Exceeded

After making 10 requests within a minute:

```bash
curl -X POST http://localhost:3000/api/slidev/generate \
  -H "Content-Type: application/json" \
  -d '{
    "sessionId": "550e8400-e29b-41d4-a716-446655440000",
    "templateId": "starter",
    "data": {}
  }'
```

**Response (429):**
```json
{
  "message": "Too many generation requests, please try again later"
}
```

**Headers:**
```
RateLimit-Limit: 10
RateLimit-Remaining: 0
RateLimit-Reset: 1696867260
```

## Testing the API

### Using curl

```bash
# Basic test
curl -v -X POST http://localhost:3000/api/slidev/generate \
  -H "Content-Type: application/json" \
  -d @- << EOF
{
  "sessionId": "550e8400-e29b-41d4-a716-446655440000",
  "templateId": "starter",
  "data": {
    "title": "Test Deck",
    "content": "Hello World"
  }
}
EOF
```

### Using httpie

```bash
http POST localhost:3000/api/slidev/generate \
  sessionId="550e8400-e29b-41d4-a716-446655440000" \
  templateId="starter" \
  data:='{"title": "Test Deck"}'
```

### Using JavaScript/TypeScript

```typescript
const response = await fetch('http://localhost:3000/api/slidev/generate', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    sessionId: '550e8400-e29b-41d4-a716-446655440000',
    templateId: 'starter',
    data: {
      title: 'My Presentation',
      subtitle: 'Built with Slidev',
    },
  }),
});

const result = await response.json();
console.log(result);
```

## Notes

- The generated deck URL points to the slide-server, which must be running separately
- The session must exist in the `conversation_sessions` table before making a request
- After successful generation, the session's `slidev_url` field is updated with the deck URL
- Template data placeholders depend on the template being used
- Currently, only the 'starter' template is supported
