import React from "react";

async function fetchData() {
  const res = await fetch("http://localhost:4000/logs", { cache: "no-cache" });

  if (!res.ok && res.status == 500) {
    throw new Error("Failed to fetch data");
  }

  if (!res.ok && res.status == 404) {
    return "";
  }

  const data = await res.json();

  return data;
}
interface LogsType {
  level: string;
  message: { method: string; timestamp: Date; url: string };
}
export default async function Page() {
  const data = await fetchData();
  return (
    <div>
      <div className="space-y-5">
        {data.logs
          .map((item: string) => JSON.parse(item))
          .map((log: LogsType, index: number) => (
            <div key={index} className="bg-white  p-4 rounded-4xl space-y-4">
              <div>Level : {log.level}</div>
              <div>message method : {log.message.method}</div>
              <div>
                message timestamp :{" "}
                {new Date(log.message.timestamp).toLocaleString("fa-IR")}
              </div>
              <div>url : {log.message.url}</div>
            </div>
          ))}
      </div>
    </div>
  );
}
