import csv
import time
from datetime import datetime
from pathlib import Path

from auracast.utils.read_temp import read_case_temp, read_cpu_temp


def main() -> None:
    script_path = Path(__file__).resolve()
    log_dir = script_path.parent

    start_time = datetime.now()
    filename = start_time.strftime("temperature_log_%Y%m%d_%H%M%S.csv")
    log_path = log_dir / filename

    with log_path.open("w", newline="") as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(["timestamp", "cpu_temp_c", "case_temp_c"])

        try:
            while True:
                now = datetime.now().isoformat(timespec="seconds")
                cpu_temp = read_cpu_temp()
                case_temp = read_case_temp()

                writer.writerow([now, cpu_temp, case_temp])
                csvfile.flush()

                time.sleep(30)
        except KeyboardInterrupt:
            pass


if __name__ == "__main__":
    main()
