/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import { Checkbox } from "@components/ui/checkbox";
import { renderComponent } from "@components/ui/data-table";
import type { ColumnDef } from "@tanstack/table-core";
import type { EventFightModel } from "./eventmodel.svelte";

export const columns: ColumnDef<EventFightModel>[] = [
    {
        id: "auswahl",
        header: ({ table }) => {
            return renderComponent(Checkbox, {
                checked: table.getIsAllRowsSelected(),
                indeterminate: table.getIsSomeRowsSelected(),
                onCheckedChange: () => {
                    if (!table.getIsSomeRowsSelected() && !table.getIsAllRowsSelected()) {
                        const now = new Date();
                        const rows = table.getRowModel().rows.filter((row) => new Date(row.original.start) > now);

                        if (rows.length > 0) {
                            rows.forEach((row) => {
                                row.toggleSelected();
                            });
                        } else {
                            table.toggleAllRowsSelected(true);
                        }
                    } else if (table.getIsSomeRowsSelected() && !table.getIsAllRowsSelected()) {
                        table.toggleAllRowsSelected(true);
                    } else {
                        table.toggleAllRowsSelected(false);
                    }
                },
            });
        },
        cell: ({ row }) => {
            return renderComponent(Checkbox, {
                checked: row.getIsSelected(),
                onCheckedChange: row.getToggleSelectedHandler(),
            });
        },
    },
    {
        accessorFn: (r) => r.blueTeam.nameWithRelation + " vs " + r.redTeam.nameWithRelation,
        id: "begegnung",
        header: "Begegnung",
    },
    {
        header: "Gruppe",
        accessorKey: "group.id",
        id: "group",
    },
    {
        header: "Datum",
        accessorKey: "start",
        id: "start",
        cell: ({ row }) => {
            return new Date(row.getValue("start")).toLocaleString("de-DE", {
                dateStyle: "short",
                timeStyle: "medium",
            });
        },
    },
    {
        header: "Spielmodus",
        accessorKey: "spielmodus",
    },
    {
        header: "Map",
        accessorKey: "map",
    },
    {
        header: "Ergebnis",
        accessorKey: "ergebnis",
        cell: ({ row }) => {
            const fight = row.original;
            if (!fight.hasFinished) {
                return "Noch nicht gespielt";
            } else if (fight.ergebnis === 1) {
                return fight.blueTeam.name + " hat gewonnen";
            } else if (fight.ergebnis === 2) {
                return fight.redTeam.name + " hat gewonnen";
            } else {
                return "Unentschieden";
            }
        },
    },
];
