from __future__ import annotations

import re
from pathlib import Path


VERSION_FILE = Path(__file__).resolve().parent.parent / "app_core" / "version.py"
VERSION_PATTERN = re.compile(r'APP_VERSION = "(\d+)\.(\d+)\.(\d+)"')


def bump_patch_version() -> str:
    content = VERSION_FILE.read_text(encoding="utf-8")
    match = VERSION_PATTERN.search(content)
    if match is None:
        raise RuntimeError(f"Could not find APP_VERSION in {VERSION_FILE}")

    major, minor, patch = (int(part) for part in match.groups())
    new_version = f"{major}.{minor}.{patch + 1}"
    updated = VERSION_PATTERN.sub(f'APP_VERSION = "{new_version}"', content, count=1)
    VERSION_FILE.write_text(updated, encoding="utf-8")
    return new_version


if __name__ == "__main__":
    print(bump_patch_version())
