# API Quick Start Guide

## ✅ Authentication is Working!

The 401 issue has been resolved. The API now accepts tokens from Keycloak.

## Quick Access

### 1. Get an Access Token

```bash
TOKEN=$(curl -s -X POST 'http://localhost:8080/realms/b2b-flight-platform/protocol/openid-connect/token' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'client_id=b2b-flight-backend' \
  -d 'client_secret=b2b-flight-backend-secret' \
  -d 'username=superadmin' \
  -d 'password=admin123' \
  -d 'grant_type=password' | jq -r '.access_token')

echo "Token: $TOKEN"
```

### 2. Use the Token

```bash
# Example: Get Dashboard
curl -X GET 'http://localhost:8081/api/backoffice/dashboard' \
  -H "Authorization: Bearer $TOKEN"

# Example: Get All Agencies
curl -X GET 'http://localhost:8081/api/agencies' \
  -H "Authorization: Bearer $TOKEN"

# Example: Create an Agency
curl -X POST 'http://localhost:8081/api/agencies' \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Test Travel Agency",
    "matriculeNumber": "TTA001",
    "contactEmail": "contact@testagency.com",
    "contactPhone": "+1234567890",
    "subscriptionType": "MONTHLY"
  }'
```

## Credentials

### Keycloak Admin
- **URL**: http://localhost:8080
- **Username**: `admin`
- **Password**: `admin`

### Test Super Admin User
- **Username**: `superadmin`
- **Password**: `admin123`
- **Role**: `SUPER_ADMIN`

### Client Credentials
- **Client ID**: `b2b-flight-backend`
- **Client Secret**: `b2b-flight-backend-secret`

## Available Endpoints

### Authentication
- `POST /api/auth/login` - Login
- `POST /api/auth/refresh` - Refresh token
- `POST /api/auth/logout` - Logout

### Agency Management (SUPER_ADMIN)
- `GET /api/agencies` - Get all agencies
- `POST /api/agencies` - Create agency
- `GET /api/agencies/{id}` - Get agency by ID
- `PUT /api/agencies/{id}` - Update agency
- `POST /api/agencies/{id}/verify` - Verify agency
- `POST /api/agencies/{id}/activate` - Activate agency
- `POST /api/agencies/{id}/deactivate` - Deactivate agency

### User Management
- `GET /api/users/{id}` - Get user
- `POST /api/users` - Create user
- `PUT /api/users/{id}` - Update user
- `DELETE /api/users/{id}` - Delete user

### Flight Operations (AGENT)
- `POST /api/flights/search` - Search flights
- `GET /api/flights/offers/{id}` - Get offer details
- `POST /api/bookings` - Create booking
- `GET /api/bookings/{id}` - Get booking

### Back Office (SUPER_ADMIN)
- `GET /api/backoffice/dashboard` - Get dashboard metrics
- `GET /api/backoffice/configuration` - Get system configuration
- `PUT /api/backoffice/configuration` - Update configuration

### Reporting (FINANCE_USER)
- `POST /api/reports/transactions` - Generate transaction report
- `POST /api/reports/financial` - Generate financial report

## Swagger UI

Access the interactive API documentation:
- **URL**: http://localhost:8081/swagger-ui.html

To use Swagger UI:
1. Get a token using the command above
2. Click the "Authorize" button in Swagger UI
3. Enter: `Bearer YOUR_TOKEN_HERE`
4. Click "Authorize"
5. Now you can test endpoints directly from Swagger

## Common Issues

### Empty Responses or 500 Errors
This is normal for a fresh installation - the database is empty. Create some data first:

```bash
# Create an agency
curl -X POST 'http://localhost:8081/api/agencies' \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "First Travel Agency",
    "matriculeNumber": "FTA001",
    "contactEmail": "info@firstagency.com",
    "contactPhone": "+1234567890",
    "subscriptionType": "MONTHLY"
  }'
```

### Token Expired
Tokens expire after a certain time. Just get a new one:

```bash
TOKEN=$(curl -s -X POST 'http://localhost:8080/realms/b2b-flight-platform/protocol/openid-connect/token' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'client_id=b2b-flight-backend' \
  -d 'client_secret=b2b-flight-backend-secret' \
  -d 'username=superadmin' \
  -d 'password=admin123' \
  -d 'grant_type=password' | jq -r '.access_token')
```

## Services Status

Check all services are running:

```bash
docker-compose ps
```

All should show "Up (healthy)":
- ✅ b2b-flight-app (port 8081)
- ✅ b2b-flight-keycloak (port 8080)
- ✅ b2b-flight-postgres (port 5432)
- ✅ b2b-flight-redis (port 6379)
- ✅ b2b-flight-rabbitmq (ports 5672, 15672)

## Next Steps

1. **Create test data** using the API endpoints
2. **Explore Swagger UI** for interactive testing
3. **Check the documentation**:
   - [Deployment Guide](docs/DEPLOYMENT.md)
   - [Developer Setup](docs/DEV-SETUP.md)
   - [API Documentation](docs/API_DOCUMENTATION.md)

## Need Help?

- Check application logs: `docker-compose logs app`
- Check Keycloak logs: `docker-compose logs keycloak`
- View all logs: `docker-compose logs -f`

---

**🎉 Your B2B Flight Ticketing Platform is ready to use!**
