#!/usr/bin/env python3
"""Rerun this use case: how a search for machine learning engineers narrows.

    export METIX_KEY=metix_xxxxxxxxxxxx   (or put it in the repository's .env)
    python3 cases/find-engineers-by-skill-and-city/fetch.py

Counts only. The ledger adds one filter at a time to the title search; the city
table repeats the title and the title plus the skill in six US cities; the
skills table counts which skills the San Francisco group lists, and the
postings table how many open US postings for the title name each tool. No
profile is read, so nothing about a person is written.
"""

from __future__ import annotations

import json
import sys
from pathlib import Path

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE.parents[1] / "tools"))

from metix_client import Platform, aggregate, load_query, suppress, write_json

SKILL = {"field": "skills", "match": "pytorch"}


def main() -> int:
    platform = Platform(max_credits=45)
    title = load_query(HERE, "ml-engineers")["where"]
    us = {"field": "location.country", "eq": "United States"}
    sf = {"field": "location.city", "eq": "San Francisco"}

    steps = [
        ("title", [title]),
        ("us", [title, us]),
        ("city", [title, us, sf]),
        ("skill", [title, us, sf, SKILL]),
    ]
    ledger = [
        {"group": group, "count": suppress(platform.count("people", {"all": clauses}))}
        for group, clauses in steps
    ]
    write_json(
        HERE,
        "ledger.json",
        aggregate("profiles", platform.snapshot, "queries/shortlist.json", ledger),
    )

    cities = json.loads((HERE / "cities.json").read_text(encoding="utf-8"))
    rows = []
    for city in cities:
        where = [title, us, {"field": "location.city", "eq": city}]
        rows.append(
            {
                "group": city,
                "count": suppress(platform.count("people", {"all": where})),
                "skill_count": suppress(platform.count("people", {"all": [*where, SKILL]})),
            }
        )
    write_json(
        HERE,
        "cities.json",
        aggregate("profiles", platform.snapshot, "queries/ml-engineers.json", rows),
    )
    # Which skills the San Francisco group lists: the field itself, then the tools.
    skills = json.loads((HERE / "skills.json").read_text(encoding="utf-8"))
    base = [title, us, sf]
    skill_rows = [
        {"group": "any", "count": suppress(platform.count("people", {"all": [*base, {"field": "skills", "exists": True}]}))}
    ] + [
        {"group": term, "count": suppress(platform.count("people", {"all": [*base, {"field": "skills", "match": term}]}))}
        for term in skills
    ]
    write_json(
        HERE,
        "skills.json",
        aggregate("profiles", platform.snapshot, "queries/shortlist.json", skill_rows, base_count=ledger[2]["count"]),
    )

    # The demand side: how many open US postings for the same title name each tool.
    posting = load_query(HERE, "postings")["where"]["all"]
    open_total = platform.count("jobs", {"all": posting})
    post_rows = [
        {"group": term, "count": platform.count("jobs", {"all": [*posting, {"field": "description", "match": term}]})}
        for term in skills
    ]
    write_json(
        HERE,
        "postings.json",
        aggregate("jobs", platform.snapshot, "queries/postings.json", post_rows, base_count=open_total),
    )

    write_json(HERE, "receipt.json", platform.receipt())
    print(ledger, rows, f"{platform.spent()} API Credits")
    return 0


if __name__ == "__main__":
    sys.exit(main())
