# Execution Workflow

## Pipeline Diagram

```mermaid
sequenceDiagram
    autonumber
    participant CLI as CLI / .env
    participant Index as index.js
    participant Preset as presets/<preset>.yaml
    participant Crawler as controllers/image-crawler.js
    participant Browser as Playwright Chromium
    participant WPController as controllers/update-wordpress.js
    participant WPService as services/wordpress.js
    participant WP as WordPress REST API

    CLI->>Index: Parse args (--url, --theme, --themes, --preset, --skip-wp)
    Index->>Preset: loadPreset(presetName)
    Preset-->>Index: { publicPages, privatePages }
    Index->>Index: Validate URL vs Theme/Themes matrix
    
    Index->>Crawler: generateImages(settings, activeThemes, publicPages, privatePages)
    activate Crawler
    Crawler->>Browser: chromium.launch()
    loop For each active theme
        loop Public pages
            Crawler->>Browser: goto(formatted URL + "?isScreenshot")
            Crawler->>Browser: screenshot(screenshots/{theme}/{theme}-{page}.png)
        end
        Crawler->>Browser: goto("en/login") & fill credentials & click submit
        loop Private pages
            Crawler->>Browser: goto(formatted URL + "?isScreenshot")
            Crawler->>Browser: screenshot(screenshots/{theme}/{theme}-{page}.png)
        end
    end
    Crawler->>Browser: close()
    Crawler-->>Index: successThemes[]
    deactivate Crawler

    alt skipWp == true (or WP_EP undefined)
        Index->>Index: Log "Skipping WordPress upload"
    else skipWp == false
        Index->>WPController: updateWordpress(wordpress, successThemes, publicPages, privatePages)
        activate WPController
        loop For each success theme
            WPController->>WPService: getCMSTheme(themeSlug)
            WPService->>WP: GET /wp/v2/cms-themes?slug={theme}
            WPController->>WPService: getPostMediaContent(themeId)
            WPService->>WP: GET /wp/v2/media?parent={themeId}
            loop Delete old images (caption contains 'demo-content-theme-image')
                WPController->>WPService: removeMedia(mediaId)
                WPService->>WP: DELETE /wp/v2/media/{id}?force=true
            end
            loop Upload new screenshots
                WPController->>WPService: uploadMedia(filePath, metadata, themeId)
                WPService->>WP: POST /wp/v2/media (file + title + caption)
                WPService->>WP: POST /wp/v2/media/{id} (associate post ID)
            end
            WPController->>WPController: buildGutenbergColumns(uploadedImages, cols=3)
            WPController->>WPService: updateCMSTheme(themeId, { content })
            WPService->>WP: POST /wp/v2/cms-themes/{id}
        end
        WPController-->>Index: complete
        deactivate WPController
    end
```

## State & Data Flow

```
[CLI / .env URL] + [presets/<preset>.yaml]
                    │
                    ▼
           Theme Matrix Check
                    │
                    ▼
         Playwright Navigation:
  {theme} in URL  => Replace {theme} + {pageURL}
  No {theme}      => Static URL + {pageURL}, store in screenshots/<theme>/
                    │
                    ▼
 [screenshots/{theme}/{theme}-{page}.png]
                    │
                    ▼
           Skip WP Upload?
           ├── Yes ──> Complete pipeline
           └── No  ──> Purge old media -> Upload -> Generate 3-col Gutenberg -> Update CPT
```
