> ## Documentation Index
> Fetch the complete documentation index at: https://docs.searchcompany.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Overview

> Architecture for the AI Articles Quota endpoint

## Purpose

Calculates how many AI articles to create today to hit the weekly target of 100 pages. Handles product rotation when there are more products than the weekly product allocation.

## Architecture

```mermaid theme={null}
flowchart TD
    Request["GET /api/cron/ai-articles-quota?org_slug=..."]
    
    subgraph calculation [Quota Calculation]
        GetEntities["Get entities"]
        GetWeekBounds["Get week bounds"]
        CountBusiness["Count business pages"]
        SelectProducts["Select products (round-robin)"]
        CountProducts["Count product pages"]
        CalcQuota["Calculate daily quota"]
    end
    
    subgraph db [Database]
        Entities["entities table"]
        AIArticles["ai_articles table"]
    end
    
    Request --> GetEntities
    GetEntities --> Entities
    GetEntities --> GetWeekBounds
    GetWeekBounds --> CountBusiness
    CountBusiness --> AIArticles
    CountBusiness --> SelectProducts
    SelectProducts --> CountProducts
    CountProducts --> CalcQuota
    CalcQuota --> Response
```

## Weekly Target Distribution

| Entity   | Weekly Target  | Notes                          |
| -------- | -------------- | ------------------------------ |
| Business | 50 pages       | 50% of 100                     |
| Products | 50 pages total | Split across selected products |

If no products exist, business gets all 100 pages.

## Round-Robin Product Selection

When there are more than 50 products:

1. Select 50 products per week using rotation
2. Week number determines which 50 are selected
3. All products eventually get coverage

## Response Format

```json theme={null}
{
  "org_slug": "org-slug-123",
  "week_start": "2024-01-15",
  "week_end": "2024-01-21",
  "week_number": 3,
  "days_remaining": 4,
  "weekly_target": 100,
  "total_products": 75,
  "products_selected_this_week": 50,
  "business": {
    "daily_quota": 8,
    "this_week": 18,
    "target": 50,
    "remaining": 32
  },
  "products": [
    {
      "name": "Product A",
      "daily_quota": 1,
      "this_week": 0,
      "target": 1,
      "remaining": 1
    }
  ],
  "total_daily_quota": 25,
  "round_robin_note": "Rotating through 75 products, 50 selected this week"
}
```

## Daily Quota Formula

```
daily_quota = ceil(remaining / days_remaining)
```

## Code Location

```
src/app/apis/cron/boosted_pages_quota/routes.py
```
