#!/usr/bin/env python3
"""Generate bcrypt hash and JWT secret for HexaWetter admin login."""
from __future__ import annotations

import argparse
import secrets

import bcrypt


def hash_password(plain_password: str) -> str:
    return bcrypt.hashpw(plain_password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")


def docker_compose_escape(value: str) -> str:
    """Escape $ for docker compose .env interpolation ($$ -> literal $)."""
    return value.replace("$", "$$")


def main() -> None:
    parser = argparse.ArgumentParser(description="Generate HexaWetter admin credentials")
    parser.add_argument("password", help="Admin password (min. 8 characters)")
    args = parser.parse_args()
    if len(args.password) < 8:
        raise SystemExit("Password must be at least 8 characters")

    password_hash = hash_password(args.password)
    jwt_secret = secrets.token_urlsafe(48)

    print("Add these lines to your .env file:\n")
    print("ADMIN_USERNAME=admin")
    print(f"ADMIN_PASSWORD_HASH={docker_compose_escape(password_hash)}")
    print(f"ADMIN_JWT_SECRET={jwt_secret}")
    print("\n# Hinweis: bcrypt-Hashes enthalten $. Fuer docker compose muss jedes $ als $$ escaped sein.")


if __name__ == "__main__":
    main()
