set dotenv-load
set positional-arguments

enrichment_data_dir := "data/enrichment"
r2_prefix := env("R2_PREFIX", "deped-enrichments-context-v1")
osm_product_manifest := "../data-openstreetmap-ph/artifacts-current/source_manifest.json"

# Build all enabled, production-ready enrichment sources.
build *args:
    uv run --group enrich deped-enrichments build {{ args }}

# Force and time one incremental build component without publishing the bundle.
build-component component *args:
    uv run --group enrich deped-enrichments components build {{ component }} {{ args }}

# Inspect component freshness and last measured build times.
build-status *args:
    uv run --group enrich deped-enrichments components status {{ args }}

# Inspect configured enrichment source file counts.
source-counts *args:
    uv run deped-enrichments source counts {{ args }}

# Fetch Wikidata PSGC/Wikipedia links into the ignored local cache.
fetch-wikimedia-links *args:
    uv run deped-enrichments source fetch-wikimedia-links {{ args }}

# Fetch PSA OpenSTAT parent-area context into the ignored local staging cache.
fetch-openstat-area-context *args:
    uv run deped-enrichments source fetch-openstat-area-context {{ args }}

# Inspect upstream OSM source-product readiness for the school-access lane.
school-access-network-inventory *args:
    uv run deped-enrichments access inventory --osm-product-manifest {{ osm_product_manifest }} {{ args }}

# Build review-only school-access artifacts from the upstream OSM source product.
build-school-access-network *args:
    uv run deped-enrichments access build-network --osm-product-manifest {{ osm_product_manifest }} {{ args }}

# Build separate review-only evidence from OSM mapped city/town centers and v2 routes.
build-osm-mapped-isolation *args:
    uv run deped-enrichments access build-osm-mapped-isolation --osm-product-manifest {{ osm_product_manifest }} {{ args }}

# Build review-only school remoteness, power, light, and optional OSM correlations.
build-school-access-correlations *args:
    uv run --group enrich deped-enrichments correlations build-school-access {{ args }}

# Build review-only typhoon and earthquake impact rollups.
build-hazard-impact *args:
    uv run deped-enrichments insights build-hazard-impact {{ args }}

# Build environment, socioeconomic, and typhoon-impact correlation artifacts.
build-environment-hazard *args:
    uv run deped-enrichments insights build-environment-hazard {{ args }}

# Build offline coordinate candidates for flagged Public DepEd schools.
build-school-coordinate-evidence *args:
    uv run deped-enrichments coordinates build-osm-evidence --osm-product-manifest {{ osm_product_manifest }} {{ args }}

# Build offline legislative and reviewed ADM4 geometry evidence.
build-osm-area-coverage-evidence *args:
    uv run deped-enrichments areas build-osm-coverage-evidence --osm-product-manifest {{ osm_product_manifest }} {{ args }}

# Compatibility alias for the former missing-only recipe.
build-missing-school-coordinate-evidence *args:
    just build-school-coordinate-evidence {{ args }}

# Run source lint and tests.
check:
    uv run --group enrich --group workbench ruff check src tests
    uv run --group enrich --group workbench pytest

# Open a workbench notebook by target name.
notebook target:
    #!/usr/bin/env bash
    set -euo pipefail
    groups=(--group workbench)
    case "{{ target }}" in
      hub) path="notebooks/00_hub.py" ;;
      enrichment) path="notebooks/10_enrichment_explorer.py" ;;
      typhoon-history) path="notebooks/11_typhoon_history.py" ;;
      earthquake-screening) path="notebooks/12_earthquake_screening.py" ;;
      access) path="notebooks/20_school_access.py" ;;
      access-gaps) path="notebooks/21_school_access_gaps.py" ;;
      access-islands) path="notebooks/22_school_island_access.py" ;;
      access-low-light) path="notebooks/23_school_access_low_light.py" ;;
      access-centers) path="notebooks/24_school_network_centers.py" ;;
      access-network-qa) path="notebooks/25_school_access_network_qa.py" ;;
      access-hazard) path="notebooks/26_school_access_hazard.py" ;;
      access-terrain-water) path="notebooks/27_school_access_terrain_water.py" ;;
      access-priority) path="notebooks/28_school_access_priority.py" ;;
      coordinates-osm) path="notebooks/29_osm_coordinate_extraction.py" ;;
      access-correlations) path="notebooks/30_school_access_correlations.py" ;;
      hazard-impact) path="notebooks/31_hazard_impact_review.py" ;;
      environment-hazard) path="notebooks/32_environment_hazard_context.py" ;;
      osm-mapped-isolation) path="notebooks/33_osm_mapped_isolation.py" ;;
      entity-mapped-access) path="notebooks/34_entity_mapped_access_relationships.py" ;;
      *) printf 'unknown notebook target: %s\n' "{{ target }}" >&2; exit 2 ;;
    esac
    uv run "${groups[@]}" marimo edit "$path"

# Static-check workbench notebooks.
check-notebooks:
    uv run --group workbench marimo check notebooks/*.py

# Serve documentation locally.
docs:
    uv run zensical serve --dev-addr localhost:8003

# Build documentation locally.
docs-build:
    uv run zensical build

publish:
    uv build --clear --no-sources
    UV_PUBLISH_TOKEN=$PYPI_TOKEN uv publish

# Create .env file from the example template.
dumpenv:
    op inject -i env.example -o .env

# Download ignored source artifacts from Cloudflare R2.
download:
    uv run --group data deped-runtime r2 download --target {{ enrichment_data_dir }} --prefix {{ r2_prefix }}

# Upload ignored source artifacts to Cloudflare R2.
upload:
    uv run --group data deped-runtime r2 upload --source {{ enrichment_data_dir }} --prefix {{ r2_prefix }}
