import os
from datetime import datetime
from pathlib import Path


def update_file(path: Path, content: str):
    existing_content = get_existing_content(path)
    if content != existing_content:
        with open(path, "w") as writer:
            writer.write("# GENERATED BY SERVEY-STUB\n")
            writer.write("# Manual updates to this file may be lost. \n")
            writer.write("# Updated At: ")
            writer.write(datetime.now().isoformat())
            writer.write("\n")
            writer.write(content)


def read_file(path: Path):
    with open(path, "r") as reader:
        return "".join(reader.readlines())


def get_existing_content(path: Path):
    if not os.path.exists(path):
        return
    with open(path, "r") as reader:
        lines = reader.read().split("\n")
        lines = [line for line in lines if not line.startswith("# ")]
    result = "\n".join(lines)
    return result
