#!/bin/sh
# Deterministic mock for scorpiox-google-quota used by tests/run.sh.
acct=""
while [ $# -gt 0 ]; do
    case "$1" in
        --account) acct="$2"; shift 2 ;;
        --json) shift ;;
        *) shift ;;
    esac
done

# Verify the plugin sanitized inherited token/profile overrides.
if [ -n "${GOOGLE_ACCESS_TOKEN:-}" ]; then
    echo "error: GOOGLE_ACCESS_TOKEN leaked into child env" >&2
    exit 1
fi
if [ "${GOOGLE_TOKEN_SOURCE:-}" != "local" ]; then
    echo "error: expected GOOGLE_TOKEN_SOURCE=local" >&2
    exit 1
fi

echo "INFO : ya29.MOCK-TOKEN-MUST-NOT-LEAK" >&2

RST_CLAUDE="${MOCK_RESET_CLAUDE:-2026-11-25T06:00:00Z}"
RST_GEMINI="${MOCK_RESET_GEMINI:-2026-11-25T03:00:00Z}"

case "$acct" in
    alpha@example.com)
        cat <<EOF
{
    "ok": true,
    "email": "alpha@example.com",
    "project": "proj-alpha",
    "endpoint": "https://daily-cloudcode-pa.googleapis.com/v1internal:retrieveUserQuota",
    "buckets": [
        {"tokenType": "WTUS", "modelId": "chat_20706", "remainingFraction": 1},
        {"resetTime": "$RST_CLAUDE", "tokenType": "WTUS", "modelId": "claude-opus-4-6-thinking", "remainingFraction": 1},
        {"resetTime": "$RST_CLAUDE", "tokenType": "WTUS", "modelId": "claude-opus-5-5-high", "remainingFraction": 1},
        {"resetTime": "$RST_CLAUDE", "tokenType": "WTUS", "modelId": "claude-sonnet-4-6", "remainingFraction": 1},
        {"resetTime": "$RST_CLAUDE", "tokenType": "WTUS", "modelId": "claude-sonnet-5-5-high", "remainingFraction": 1},
        {"resetTime": "$RST_GEMINI", "tokenType": "WTUS", "modelId": "gemini-3.8-flash-tiered", "remainingFraction": 0.855},
        {"resetTime": "$RST_GEMINI", "tokenType": "WTUS", "modelId": "gemini-4-argon-high", "remainingFraction": 1},
        {"resetTime": "$RST_GEMINI", "tokenType": "WTUS", "modelId": "gemini-4-hydrogen-high", "remainingFraction": 0.855},
        {"resetTime": "$RST_GEMINI", "tokenType": "WTUS", "modelId": "gemini-4-hydrogen-sonar", "remainingFraction": 0.855},
        {"resetTime": "$RST_GEMINI", "tokenType": "WTUS", "modelId": "gemini-4-neon-high", "remainingFraction": 0.855},
        {"tokenType": "WTUS", "modelId": "tab_flash_lite_preview", "remainingFraction": 1}
    ],
    "summary": {
        "groups": [
            {
                "displayName": "Gemini Models",
                "buckets": [
                    {"bucketId": "gemini-weekly", "window": "weekly", "remainingFraction": 0.855, "resetTime": "$RST_GEMINI"},
                    {"bucketId": "gemini-5h", "window": "5h", "remainingFraction": 0.375, "resetTime": "$RST_CLAUDE"}
                ]
            },
            {
                "displayName": "Claude and GPT models",
                "buckets": [
                    {"bucketId": "3p-weekly", "window": "weekly", "remainingFraction": 0.99, "resetTime": "$RST_GEMINI"},
                    {"bucketId": "3p-5h", "window": "5h", "remainingFraction": 1}
                ]
            }
        ]
    }
}
EOF
        exit 0
        ;;
    beta@example.com)
        cat <<EOF
{
    "ok": true,
    "email": "beta@example.com",
    "project": "aicode-consumers",
    "endpoint": "https://daily-cloudcode-pa.googleapis.com/v1internal:retrieveUserQuota",
    "buckets": [
        {"resetTime": "$RST_CLAUDE", "tokenType": "WTUS", "modelId": "claude-sonnet-4-6", "remainingFraction": 0.42},
        {"resetTime": "$RST_GEMINI", "tokenType": "WTUS", "modelId": "gemini-3.8-flash-tiered", "remainingFraction": null},
        {"resetTime": "$RST_GEMINI", "tokenType": "WTUS", "modelId": "gemini-4-hydrogen-sonar"}
    ],
    "summary": {
        "groups": [
            {
                "displayName": "Gemini Models",
                "buckets": [
                    {"bucketId": "gemini-weekly", "window": "weekly", "resetTime": "$RST_GEMINI"}
                ]
            },
            {
                "displayName": "Claude and GPT models",
                "buckets": [
                    {"bucketId": "3p-weekly", "window": "weekly", "remainingFraction": 0.42, "resetTime": "$RST_GEMINI"},
                    {"bucketId": "3p-5h", "window": "5h", "remainingFraction": 0.50, "resetTime": "$RST_CLAUDE"}
                ]
            }
        ]
    }
}
EOF
        exit 0
        ;;
    broken@example.com)
        echo "error: invalid_grant" >&2
        exit 1
        ;;
    *)
        echo "error: unknown mock account $acct" >&2
        exit 1
        ;;
esac
