import { useTranslation } from "react-i18next";

import { useContactId } from "@/hooks/use-contact-id";
import { TableInfinity } from "@/shared";

import { formatDateTime } from "../../../services/time.service";
import { useNames } from "../hooks/useNames";
import { useWatcherStore } from "../store/watcher.store";

import styles from "./FaultTable.module.css";

export function FaultTable() {
	const { t } = useTranslation("translation");
	const { getMessage } = useContactId();
	const watcher = useWatcherStore();
	const names = useNames();

	function getData() {
		return watcher.assetFaults().map((x) => ({
			values: [
				formatDateTime(new Date(x.event.createdAt)),
				getMessage(x.event.code),
				x.event.areaIndex?.toFixed(0) ?? "-",
				names.getAreaName(x.event.areaIndex),
			],
			color: "var(--error-300)",
		}));
	}

	return (
		<div className={styles.container}>
			<div className={styles.title}>
				<span>{t("Total: ")}</span>
				<span className={styles.count}>
					{watcher.assetEvents.length}
				</span>
				<span>{t("Faults")}</span>
			</div>
			<TableInfinity
				columns={[
					{
						label: t("timeOfReceiving"),
						width: 1,
					},
					{
						label: t("fault"),
						width: 3,
					},
					{
						label: t("area"),
						width: 0.5,
					},
					{
						label: t("areaName"),
						width: 2,
					},
				]}
				rows={getData()}
			/>
		</div>
	);
}
