#!/usr/bin/env python3
from __future__ import annotations

import json
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def _read(path: Path) -> str:
    return path.read_text(encoding="utf-8", errors="replace") if path.exists() else ""


def main() -> int:
    files = {
        "workflow": ROOT / ".gitea" / "workflows" / "qualitative_sell_strategy.yml",
        "build_inputs": ROOT / "tools" / "build_qualitative_sell_inputs_v1.py",
        "build_satellite": ROOT / "tools" / "build_satellite_candidate_recommendations_v1.py",
        "evaluate": ROOT / "tools" / "evaluate_qualitative_sell_strategy_accuracy_v1.py",
        "store": ROOT / "src" / "quant_engine" / "qualitative_sell_strategy_store_v1.py",
        "package": ROOT / "package.json",
    }
    errors: list[str] = []

    for name, path in files.items():
        if not path.exists():
            errors.append(f"missing:{name}")

    checks = {
        "build_inputs_flags": ("--store-backend" in _read(files["build_inputs"]) and "--store-location" in _read(files["build_inputs"])),
        "build_satellite_flags": ("--store-backend" in _read(files["build_satellite"]) and "--store-location" in _read(files["build_satellite"])),
        "evaluate_flags": ("--store-backend" in _read(files["evaluate"]) and "--store-location" in _read(files["evaluate"])),
        "store_contract": ("resolve_store_path" in _read(files["store"]) and "QualitativeSellStoreSpec" in _read(files["store"])),
        "workflow_mentions_mock_validation": ("validate_kis_api_credentials_v1.py" in _read(files["workflow"])),
        "workflow_has_schedule": ("schedule:" in _read(files["workflow"]) and "workflow_dispatch:" in _read(files["workflow"])),
        "package_scripts": ("ops:sell-build" in _read(files["package"]) and "ops:sell-eval" in _read(files["package"]) and "ops:sell-validate" in _read(files["package"])),
    }

    for key, ok in checks.items():
        if not ok:
            errors.append(key)

    result = {
        "formula_id": "QUALITATIVE_SELL_STRATEGY_PIPELINE_V1",
        "gate": "PASS" if not errors else "FAIL",
        "checks": checks,
        "errors": errors,
    }
    out = ROOT / "Temp" / "qualitative_sell_strategy_pipeline_v1.json"
    out.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
    print(json.dumps(result, ensure_ascii=False, indent=2))
    return 0 if not errors else 1


if __name__ == "__main__":
    raise SystemExit(main())
