# Workflow

## 1. Server Mode

```mermaid
flowchart TD
    A1[systemd timer / manual run] --> A2[run_update_and_commit.sh config.server.json]
    A3[manual updater run] --> A4[domain_updater.py --config config.server.json]

    subgraph U1["domain_updater.py / server mode"]
      direction TB
      B1[read config.server.json] --> B2[resolve output paths]
      B2 --> B3[read runtime/state.json]
      B3 --> B4[fetch API payload]
      B4 --> B5[parse candidates]
      B5 --> B6[apply domain filter]
      B6 --> B7[apply record filter]
      B7 --> B8[score and rank]
      B8 --> B9[optional healthcheck]
      B9 --> B10[select preferred domain]
      B10 --> B11[write runtime/current_domain.txt]
      B11 --> B12[write runtime/current_domain.json]
      B12 --> B13[write runtime/substore_vars.json]
      B13 --> B14[write runtime/state.json]
    end

    A2 --> C1[resolve configured output paths]
    C1 --> A4
    A4 --> C2[read selected text file from resolved path]
    C2 --> C3[compare with runtime-state HEAD]
    C3 --> C4{changed?}
    C4 -- no --> C5[skip commit/push]
    C4 -- yes --> C6[sync configured repo-local output files]
    C6 --> C7[commit on runtime-state]
    C7 --> C8[optional push]
```

## 2. Local Python cfst Mode

```mermaid
flowchart TD
    A1[manual run] --> A2[domain_updater.py --config config.router.json]

    subgraph U2["domain_updater.py / cfst_local mode"]
      direction TB
      B1[read config.router.json] --> B2[resolve output paths]
      B2 --> B3[run local cfst]
      B3 --> B4[parse result.csv]
      B4 --> B5[optional filter / optional healthcheck]
      B5 --> B6[select preferred ip]
      B6 --> B7[write cfip_runtime/current_ip.txt]
      B7 --> B8[write cfip_runtime/current_ip.json]
      B8 --> B9[write cfip_runtime/substore_vars.json]
      B9 --> B10[write cfip_runtime/state.json]
    end
```

## 3. BusyBox Router Mode

```mermaid
flowchart TD
    A1[crond / manual run] --> A2[router_local_update.sh router_local.conf]
    A3[background service / manual run] --> A4[router_local_http.sh router_local.conf]

    subgraph R1["router_local_update.sh"]
      direction TB
      B1[read router_local.conf] --> B2[run cfst]
      B2 --> B3[read result.csv]
      B3 --> B4[pick best ip]
      B4 --> B5[write cfip_runtime/current_ip.txt]
      B5 --> B6[write cfip_runtime/current_ip.json]
      B6 --> B7[write cfip_runtime/substore_vars.json]
      B7 --> B8[write cfip_runtime/state.json]
    end

    subgraph R2["router_local_http.sh"]
      direction TB
      C1[read router_local.conf] --> C2[start configured BusyBox httpd]
      C2 --> C3[serve current_ip.txt]
      C2 --> C4[serve current_ip.json]
      C2 --> C5[serve state.json]
      C2 --> C6[serve substore_vars.json]
    end
```

## 4. Consumers

```mermaid
flowchart TD
    A1[runtime/current_domain.json] --> B1[Sub-Store operator]
    A2[cfip_runtime/current_ip.json over LAN] --> B1
    B1 --> C1[read domain or ip]
    C1 --> C2[rewrite matched vmess server]

    D1[runtime/current_domain.txt] --> E1[update_vmess_links.py]
    D2[cfip_runtime/current_ip.txt] --> E1
    E1 --> E2[rewrite vmess add field]
```
