"use client";

import { Check, MessageSquareWarning } from "lucide-react";
import { MessageForm } from "./_components/messageForm";
import { PaginatedTable } from "_components/PaginatedTable";
import { ColumnDef } from "@tanstack/react-table";
import { Notam } from "@repo/db";

export default function MessagePage() {
	return (
		<>
			<div className="grid grid-cols-6 gap-4">
				<div className="col-span-full">
					<p className="text-2xl font-semibold text-left flex items-center gap-2">
						<MessageSquareWarning className="w-5 h-5" /> Service Nachrichten
					</p>
				</div>
				<div className="card bg-base-200 shadow-xl mb-4 col-span-6">
					<div className="card-body">
						<MessageForm />
					</div>
				</div>
			</div>
			<PaginatedTable
				prismaModel="notam"
				initialOrderBy={[{ id: "createdAt", desc: true }]}
				columns={
					[
						{
							accessorKey: "message",
							header: "Nachricht",
						},
						{
							accessorKey: "color",
							header: "Typ",
							cell: ({ row }) => {
								const color = row.getValue("color");
								return color;
							},
						},
						{
							accessorKey: "wartungsmodus",
							header: "Wartungsmodus",
							cell: ({ row }) => {
								const wartungsmodus = row.getValue("wartungsmodus");
								return wartungsmodus ? <Check /> : "";
							},
						},
						{
							accessorKey: "disableHPG",
							header: "HPG deaktiviert",
							cell: ({ row }) => {
								const disableHPG = row.getValue("disableHPG");
								return disableHPG ? <Check /> : "";
							},
						},
						{
							accessorKey: "showUntil",
							header: "Zeitlimit",
							cell: ({ row, cell }) => {
								const showUntil = new Date(cell.getValue() as string);
								const createdAt = new Date(row.getValue("createdAt") as string);
								if (showUntil > createdAt) {
									return showUntil.toLocaleDateString("de-DE", {
										year: "numeric",
										month: "2-digit",
										day: "2-digit",
										hour: "2-digit",
										minute: "2-digit",
									});
								}
								return "";
							},
						},
						{
							accessorKey: "createdAt",
							header: "Erstellt am",
							cell: ({ cell }) => {
								const date = new Date(cell.getValue() as string);
								return date.toLocaleDateString("de-DE", {
									year: "numeric",
									month: "2-digit",
									day: "2-digit",
									hour: "2-digit",
									minute: "2-digit",
								});
							},
						},
					] as ColumnDef<Notam>[]
				}
			/>
		</>
	);
}
