from pathlib import Path


REQUIRED_FILES = [
    "app.py",
    "passenger_wsgi.py",
    "requirements.txt",
    ".env.example",
    "init_db.py",
    "CPANEL_DEPLOY_TUTORIAL.md",
]


def main():
    root = Path(__file__).resolve().parent
    missing = [name for name in REQUIRED_FILES if not (root / name).exists()]

    print("SPANDAMAN Hostable Portal cPanel Prep Check")
    print(f"Root: {root}")
    print()

    if missing:
        print("Missing required files:")
        for name in missing:
            print(f"- {name}")
    else:
        print("Required deployment files: OK")

    env_path = root / ".env"
    if env_path.exists():
        content = env_path.read_text(encoding="utf-8", errors="ignore")
        secret_changed = "change-this-to-a-long-random-secret" not in content
        secure_session = "SESSION_COOKIE_SECURE=true" in content
        secure_remember = "REMEMBER_COOKIE_SECURE=true" in content
        print(f".env present: YES")
        print(f"SECRET_KEY changed: {'YES' if secret_changed else 'NO'}")
        print(f"SESSION_COOKIE_SECURE=true: {'YES' if secure_session else 'NO'}")
        print(f"REMEMBER_COOKIE_SECURE=true: {'YES' if secure_remember else 'NO'}")
    else:
        print(".env present: NO")

    db_candidates = [root / "portal.db", root / "instance" / "portal.db"]
    db_found = next((path for path in db_candidates if path.exists()), None)
    print(f"Database file found: {db_found if db_found else 'NO'}")

    venv_python = root / "venv" / "Scripts" / "python.exe"
    print(f"Local Windows venv present: {'YES' if venv_python.exists() else 'NO'}")
    print()
    print("CPANEL_PREP_CHECK_DONE")


if __name__ == "__main__":
    main()
