# Strapi Integration Guide

## Setup

1. Start Strapi CMS:
```bash
cd cms/cms
npm run develop
```

2. Configure environment variables:
- In `backend/.env`, add your Strapi API token (create one in Strapi admin panel)

## Content Types

Create these content types in Strapi admin panel:

### Article
- Fields:
  - `title` (Text, required)
  - `description` (Text)
  - `content` (Rich Text, required)
  - `slug` (UID, target field: title)
  - `publishedAt` (DateTime)
  - `author` (Relation with Author)
  - `category` (Relation with Category)
  - `tags` (Relation with Tag)
- Settings: Draft & publish enabled

### Author
- Fields:
  - `name` (Text, required)
  - `bio` (Text)
  - `avatar` (Media)

### Category
- Fields:
  - `name` (Text, required)
  - `description` (Text)
  - `parent` (Relation with Category, self-referential)

### Tag
- Fields:
  - `name` (Text, required)

## Webhook Configuration

In Strapi admin panel, go to Settings → Webhooks and create a webhook:

### Article Sync Webhook
- **Name**: Backend Article Sync
- **URL**: `http://localhost:3000/api/v1/webhooks/strapi/article`
- **Events**: 
  - ✓ entry.create
  - ✓ entry.update
  - ✓ entry.delete
- **Headers**: None required for now
- **Send headers**: No

### API Token Creation
1. Go to Settings → API Tokens → Create new API Token
2. Name: Backend Integration
3. Duration: Unlimited
4. Token Type: Full access (or customize as needed)
5. Copy the generated token to `backend/.env` as `STRAPI_API_TOKEN`

## Manual Sync

Trigger a full sync manually:
```bash
curl -X POST http://localhost:3000/api/v1/webhooks/strapi/sync/all
```

## Data Flow

1. Content is created/updated in Strapi
2. Strapi sends webhook to backend
3. Backend fetches the updated content from Strapi API
4. Backend syncs content to local SQLite database
5. Frontend queries backend API for public content

## Testing the Integration

1. Create an article in Strapi
2. Publish it
3. Check if it appears in backend: `curl http://localhost:3000/api/v1/articles`
4. Frontend should now display the article

## Troubleshooting

### Webhook not triggering
- Check Strapi webhook logs in admin panel
- Verify backend is running on the correct URL
- Check network connectivity

### Sync not working
- Verify STRAPI_API_TOKEN is correct
- Check Strapi content type permissions
- Review backend logs for errors
- Test Strapi API directly: `curl http://localhost:1337/api/articles`

### CORS errors
- Ensure backend CORS includes Strapi URL
- Update `backend/src/main.ts` CORS configuration
