from pathlib import Path

root = Path(__file__).resolve().parent

required = [
    root / "core" / "config.py",
    root / "core" / "database.py",
    root / "services" / "order_service.py",
    root / "services" / "product_pricing_service.py",
    root / "services" / "iran_usdt_rate_service.py",
    root / "services" / "tabdeal_rate_service.py",
    root / "states" / "order.py",
]

print("Project root:", root)
print()

missing = [
    path
    for path in required
    if not path.exists()
]

if missing:
    print("MISSING FILES:")
    for path in missing:
        print(" -", path.relative_to(root))
    raise SystemExit(1)

order_service_text = (
    root
    / "services"
    / "order_service.py"
).read_text(
    encoding="utf-8",
    errors="replace",
)

config_text = (
    root
    / "core"
    / "config.py"
).read_text(
    encoding="utf-8",
    errors="replace",
)

checks = {
    "STATUS_CANCELLED_BY_USER":
        "STATUS_CANCELLED_BY_USER"
        in order_service_text,

    "accept_price_change":
        "async def accept_price_change("
        in order_service_text,

    "cancel_price_change":
        "async def cancel_price_change("
        in order_service_text,

    "old MarketApp warning removed":
        (
            "MARKET_APP_TOKEN is not configured. "
            "MarketApp operations may fail."
        )
        not in config_text,
}

failed = False

for name, ok in checks.items():
    print(
        ("OK   " if ok else "FAIL "),
        name,
    )
    if not ok:
        failed = True

print()

if failed:
    print(
        "Some project files are still old. "
        "Replace the failed files from the hotfix pack."
    )
    raise SystemExit(2)

print("Hotfix files look correct.")
print("Now run: python main.py")