<script lang="ts">
    import type { EventFight, EventFightEdit, ResponseGroups, ResponseRelation, SWEvent } from "@type/event";
    import { Button } from "@components/ui/button";
    import { EditIcon, CopyIcon } from "lucide-svelte";
    import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@components/ui/dialog";
    import FightEdit from "@components/moderator/components/FightEdit.svelte";
    import type { Team } from "@components/types/team";
    import { fightRepo } from "@components/repo/fight";
    import { eventRepo } from "@components/repo/event";
    import type { EventModel } from "./eventmodel.svelte";

    let { fight, onupdate, refresh, data }: { fight: EventFight; onupdate: (update: EventFight) => void; refresh: () => void; data: EventModel } = $props();

    let editOpen = $state(false);
    let duplicateOpen = $state(false);

    async function handleSave(fightData: EventFightEdit) {
        let f = await $fightRepo.updateFight(data.event.id, fight.id, {
            ...fightData,
            blueTeam: fightData.blueTeam.id,
            redTeam: fightData.redTeam.id,
            group: fightData.group ?? -1,
        });

        onupdate(f);

        editOpen = false;
    }

    async function handlyCopy(fightData: EventFightEdit) {
        await $eventRepo.createFight(data.event.id.toString(), {
            ...fightData,
            blueTeam: fightData.blueTeam.id,
            redTeam: fightData.redTeam.id,
        });

        refresh();

        duplicateOpen = false;
    }

    async function handleDelete() {
        await $fightRepo.deleteFight(data.event.id, fight.id);
        refresh();
    }
</script>

<div>
    <Dialog bind:open={editOpen}>
        <DialogTrigger>
            <Button variant="ghost" size="icon">
                <EditIcon />
            </Button>
        </DialogTrigger>
        <DialogContent>
            <DialogHeader>
                <DialogTitle>Fight bearbeiten</DialogTitle>
                <DialogDescription>Hier kannst du die Daten des Kampfes bearbeiten.</DialogDescription>
            </DialogHeader>
            <FightEdit {fight} {data} onSave={handleSave}>
                {#snippet actions(dirty, submit)}
                    <DialogFooter>
                        <Button variant="destructive" onclick={handleDelete}>Löschen</Button>
                        <Button disabled={!dirty} onclick={submit}>Speichern</Button>
                    </DialogFooter>
                {/snippet}
            </FightEdit>
        </DialogContent>
    </Dialog>
    <Dialog bind:open={duplicateOpen}>
        <DialogTrigger>
            <Button variant="ghost" size="icon">
                <CopyIcon />
            </Button>
        </DialogTrigger>
        <DialogContent>
            <DialogHeader>
                <DialogTitle>Fight duplizieren</DialogTitle>
                <DialogDescription>Hier kannst du die Daten des duplizierten Fights ändern</DialogDescription>
            </DialogHeader>
            <FightEdit {fight} {data} onSave={handlyCopy}>
                {#snippet actions(dirty, submit)}
                    <DialogFooter>
                        <Button onclick={submit}>Speichern</Button>
                    </DialogFooter>
                {/snippet}
            </FightEdit>
        </DialogContent>
    </Dialog>
</div>
