from __future__ import annotations

import argparse
import sys
from pathlib import Path


ROOT_DIR = Path(__file__).resolve().parent.parent
if str(ROOT_DIR) not in sys.path:
    sys.path.insert(0, str(ROOT_DIR))

from core.tenant_manager import TenantManager


def main() -> int:
    parser = argparse.ArgumentParser(description="Generate an offline activation code for a device ID.")
    parser.add_argument("device_id", nargs="?", help="Device ID shown by the desktop app")
    args = parser.parse_args()

    device_id = str(args.device_id or "").strip()
    if not device_id:
        try:
            device_id = input("Device ID: ").strip()
        except EOFError:
            device_id = ""

    if not device_id:
        print("Device ID gerekli.", file=sys.stderr)
        return 1

    code = TenantManager().generate_offline_mode_code(device_id)
    if not code:
        print("Offline code could not be generated.", file=sys.stderr)
        return 1

    print(code)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
input("Press Enter to exit...")

