# Swagger UI Integration

This project now includes Swagger UI for API documentation and testing.

## Features

- **Interactive API Documentation**: View all API endpoints with detailed descriptions
- **Request/Response Examples**: See example requests and responses for each endpoint
- **Try It Out**: Test API endpoints directly from the browser
- **Authentication Support**: JWT Bearer token authentication for protected endpoints
- **Comprehensive Coverage**: Documents all existing API endpoints

## Accessing Swagger UI

Once the server is running, you can access the Swagger UI at:

```
http://localhost:3000/api-docs
```

## API Endpoints Documented

### Authentication

- `POST /v1/auth/register` - User registration
- `POST /v1/auth/login` - User login

### AI Chat Room

- `POST /v1/ai-chat/chat` - Send message to AI (requires authentication)

### Newsletter

- `POST /v1/newsletter/subscribe` - Subscribe to newsletter

### Contact Support

- `POST /v1/contact-support/submit` - Submit support ticket

## Using Swagger UI

1. **Browse Endpoints**: Navigate through different API sections using the tags
2. **View Details**: Click on any endpoint to see detailed information
3. **Try Endpoints**: Use the "Try it out" button to test endpoints
4. **Authentication**: For protected endpoints, click the "Authorize" button and enter your JWT token
5. **Response Examples**: See example responses and status codes

## Authentication

For protected endpoints (like AI Chat), you'll need to:

1. Click the "Authorize" button at the top of the page
2. Enter your JWT token in the format: `Bearer YOUR_TOKEN_HERE`
3. Click "Authorize"
4. Now you can test protected endpoints

## Development

### Adding New Endpoints

To document new endpoints:

1. Add the endpoint details to `swagger.yaml`
2. Follow the existing format and structure
3. Include request/response schemas
4. Add appropriate tags and descriptions

### Customization

The Swagger UI is configured with:

- Custom title: "Schematics Backend API Documentation"
- Hidden topbar for cleaner appearance
- Expanded endpoint list by default
- Filter functionality enabled
- Try-it-out functionality enabled

## Dependencies

The following packages were added:

- `swagger-ui-express`: Serves the Swagger UI
- `yamljs`: Parses the YAML specification file
- `@types/swagger-ui-express`: TypeScript definitions
- `@types/yamljs`: TypeScript definitions

## File Structure

```
├── swagger.yaml              # OpenAPI specification
├── src/
│   ├── server.ts            # Server with Swagger UI integration
│   └── types/
│       └── swagger.d.ts     # Type declarations
└── SWAGGER_README.md        # This file
```

## Troubleshooting

### TypeScript Errors

If you encounter TypeScript errors, ensure the type declarations in `src/types/swagger.d.ts` are properly loaded.

### Swagger UI Not Loading

Check that:

1. The `swagger.yaml` file exists in the project root
2. All dependencies are installed
3. The server is running on the expected port

### YAML Parsing Issues

Ensure your `swagger.yaml` file follows the OpenAPI 3.0 specification format.
