#!/usr/bin/env python3
"""Reset optional Shabbat shiur settings in display.json."""

from __future__ import annotations

import json
import sys
from pathlib import Path


def clear_shabbat_shiur(config_path: Path) -> bool:
    with config_path.open(encoding="utf-8") as f:
        config = json.load(f)

    changed = config.get("shabbatShiurTitle") != "" or config.get("shabbatShachritEventTitle") != ""

    config["shabbatShiurTitle"] = ""
    config["shabbatShachritEventTitle"] = ""

    if not changed:
        print(f"No changes needed: {config_path}")
        return False

    with config_path.open("w", encoding="utf-8", newline="\n") as f:
        json.dump(config, f, ensure_ascii=False, indent=2)
        f.write("\n")

    print(f"Cleared Shabbat shiur settings in {config_path}")
    return True


def main() -> int:
    config_path = (
        Path(sys.argv[1]).expanduser().resolve()
        if len(sys.argv) > 1
        else Path(__file__).resolve().parent / "../display.json"
    )

    if not config_path.is_file():
        print(f"Config file not found: {config_path}", file=sys.stderr)
        return 1

    try:
        clear_shabbat_shiur(config_path)
    except (OSError, json.JSONDecodeError) as err:
        print(f"Failed to update {config_path}: {err}", file=sys.stderr)
        return 1

    return 0


if __name__ == "__main__":
    raise SystemExit(main())
