flowchart TD
    A[CLI Entry Point\nsrc/cli/train.py] --> B[Parse Arguments\n--model, --epochs, --batch, --imgsz, etc.]
    B --> C[Connect PostgreSQL\nDB_HOST / DB_NAME / DB_PASSWORD]

    C --> D[Load Data from DB\nsrc/yolo/db_dataset.py]
    D --> D1[Scan temp/doc_id/images/\nfor rendered PNGs]
    D --> D2[Batch load field_results\nfrom database - batch 500]

    D1 --> E[Create DBYOLODataset]
    D2 --> E

    E --> F[Split Train/Val/Test\n80% / 10% / 10%\nDocument-level, seed=42]

    F --> G[Export to YOLO Format]
    G --> G1[Copy images to\ntrain/val/test dirs]
    G --> G2[Generate .txt labels\nclass x_center y_center w h]
    G --> G3[Generate dataset.yaml\n+ classes.txt]
    G --> G4[Coordinate Conversion\nPDF points 72DPI -> render DPI\nNormalize to 0-1]

    G1 --> H{--export-only?}
    G2 --> H
    G3 --> H
    G4 --> H

    H -- Yes --> Z[Done - Dataset exported]
    H -- No --> I[Load YOLO Model]

    I --> I1{--resume?}
    I1 -- Yes --> I2[Load last.pt checkpoint]
    I1 -- No --> I3[Load pretrained model\ne.g. yolo11n.pt]

    I2 --> J[Configure Training]
    I3 --> J

    J --> J1[Conservative Augmentation\nrotation=5 deg, translate=5%\nno flip, no mosaic, no mixup]
    J --> J2[imgsz=1280, pretrained=True]

    J1 --> K[model.train\nUltralytics Training Loop]
    J2 --> K

    K --> L[Training Outputs\nruns/train/name/]
    L --> L1[weights/best.pt\nweights/last.pt]
    L --> L2[results.csv + results.png\nTraining curves]
    L --> L3[PR curves, F1 curves\nConfusion matrix]

    L1 --> M[Test Set Validation\nmodel.val split=test]
    M --> N[Report Metrics\nmAP@0.5 = 93.5%\nmAP@0.5-0.95]

    N --> O[Close DB Connection]

    style A fill:#4a90d9,color:#fff
    style K fill:#e67e22,color:#fff
    style N fill:#27ae60,color:#fff
    style Z fill:#95a5a6,color:#fff
