<script lang="ts">
    import * as m from "$lib/paraglide/messages";
    import KvNumberInput from "./KvNumberInput.svelte";

    interface Props {
        name: string;
        score: number;
        scoreAdjustment?: number;
        answering?: boolean;
        selectable?: boolean;
        // Final round mode
        finalMode?: boolean;
        finalJudged?: boolean;
        finalActive?: boolean;
        onFinalJudge?: (correct: boolean, wager: number) => void;
        // Event handlers
        onSelect?: () => void;
        onAdd?: () => void;
        onRemove?: () => void;
        onCorrect?: () => void;
        onWrong?: () => void;
    }

    let {
        name,
        score,
        scoreAdjustment = 100,
        answering = false,
        selectable = false,
        finalMode = false,
        finalJudged = false,
        finalActive = false,
        onFinalJudge,
        onSelect,
        onAdd,
        onRemove,
        onCorrect,
        onWrong,
    }: Props = $props();

    // Final round state
    let finalAnswerCorrect = $state<boolean | null>(null);
    let finalWagerInput = $state(0);
</script>

<div
    class="group relative bg-kv-blue flex flex-col gap-4 items-center justify-center p-4 flex-1 min-w-0 box-border
    {answering || finalActive
        ? 'border-8 border-kv-yellow'
        : 'border-8 border-transparent'}
    {selectable && !finalJudged ? 'cursor-pointer' : ''}
    {finalJudged ? 'opacity-60' : ''}"
    onclick={selectable && !finalJudged ? onSelect : undefined}
    onkeydown={selectable && !finalJudged
        ? (e) => e.key === "Enter" && onSelect?.()
        : undefined}
    role={selectable && !finalJudged ? "button" : undefined}
    tabindex={selectable && !finalJudged ? 0 : undefined}
>
    <!-- Hover overlay - darkens background only, extends to cover border -->
    {#if selectable && !finalJudged}
        <div
            class="absolute -inset-2 bg-black opacity-0 group-hover:opacity-20 transition-opacity pointer-events-none z-0"
        ></div>
    {/if}

    <!-- Name and Score -->
    <div
        class="relative z-10 flex flex-col gap-4 items-center font-kv-body text-kv-white text-center uppercase w-full
        {selectable && !finalJudged
            ? 'group-hover:text-kv-yellow transition-colors'
            : ''}"
    >
        <span class="text-2xl md:text-4xl kv-shadow-text">{name}</span>
        <span
            class="text-2xl md:text-4xl kv-shadow-text {score < 0
                ? 'text-kv-red'
                : ''}">{score}€</span
        >
        {#if finalJudged}
            <span class="text-sm text-kv-green">✓ {m.kv_play_judged()}</span>
        {/if}
    </div>

    <!-- Action Buttons -->
    <div class="relative z-10 flex flex-col gap-2 items-center justify-center">
        {#if finalMode && finalActive}
            <!-- Final round judging UI -->
            {#if finalAnswerCorrect === null}
                <div class="flex gap-2">
                    <button
                        onclick={() => (finalAnswerCorrect = true)}
                        class="bg-kv-green border-4 border-black box-border flex items-center justify-center p-2 cursor-pointer hover:opacity-80"
                    >
                        <span
                            class="font-kv-body text-xl md:text-[28px] text-kv-white uppercase kv-shadow-text"
                        >
                            {m.kv_play_correct()}
                        </span>
                    </button>
                    <button
                        onclick={() => (finalAnswerCorrect = false)}
                        class="bg-kv-red border-4 border-black box-border flex items-center justify-center p-2 cursor-pointer hover:opacity-80"
                    >
                        <span
                            class="font-kv-body text-xl md:text-[28px] text-kv-white uppercase kv-shadow-text"
                        >
                            {m.kv_play_wrong()}
                        </span>
                    </button>
                </div>
            {:else}
                <!-- Wager input and confirm -->
                <div class="flex items-center gap-2">
                    <KvNumberInput
                        bind:value={finalWagerInput}
                        min={0}
                        max={Math.max(score, 0)}
                        step={100}
                    />
                    <span class="font-kv-body text-lg text-kv-white">€</span>
                </div>
                <button
                    onclick={() => {
                        onFinalJudge?.(finalAnswerCorrect!, finalWagerInput);
                        finalAnswerCorrect = null;
                        finalWagerInput = 0;
                    }}
                    class="bg-kv-yellow border-4 border-black box-border font-kv-body text-lg text-black uppercase px-4 py-1 cursor-pointer hover:opacity-80"
                >
                    {m.kv_play_confirm()}
                </button>
            {/if}
        {:else if finalMode && !finalJudged && !finalActive}
            <!-- Final mode but not active - show score adjustment buttons -->
            <div class="flex gap-4">
                <button
                    onclick={onAdd}
                    class="bg-kv-green border-4 border-black box-border flex items-center justify-center p-2 cursor-pointer hover:opacity-80"
                >
                    <span
                        class="font-kv-body text-xl md:text-[28px] text-kv-white uppercase kv-shadow-text"
                    >
                        +{scoreAdjustment}
                    </span>
                </button>
                <button
                    onclick={onRemove}
                    class="bg-kv-red border-4 border-black box-border flex items-center justify-center p-2 cursor-pointer hover:opacity-80"
                >
                    <span
                        class="font-kv-body text-xl md:text-[28px] text-kv-white uppercase kv-shadow-text"
                    >
                        -{scoreAdjustment}
                    </span>
                </button>
            </div>
        {:else if answering}
            <!-- Correct/Wrong buttons when answering -->
            <div class="flex gap-4">
                <button
                    onclick={onCorrect}
                    class="bg-kv-green border-4 border-black box-border flex items-center justify-center p-2 cursor-pointer hover:opacity-80"
                >
                    <span
                        class="font-kv-body text-xl md:text-[28px] text-kv-white uppercase kv-shadow-text"
                    >
                        {m.kv_play_correct()}
                    </span>
                </button>
                <button
                    onclick={onWrong}
                    class="bg-kv-red border-4 border-black box-border flex items-center justify-center p-2 cursor-pointer hover:opacity-80"
                >
                    <span
                        class="font-kv-body text-xl md:text-[28px] text-kv-white uppercase kv-shadow-text"
                    >
                        {m.kv_play_wrong()}
                    </span>
                </button>
            </div>
        {:else if !finalMode}
            <!-- Add/Remove score buttons in normal mode -->
            <div class="flex gap-4">
                <button
                    onclick={onAdd}
                    class="bg-kv-green border-4 border-black box-border flex items-center justify-center p-2 cursor-pointer hover:opacity-80"
                >
                    <span
                        class="font-kv-body text-xl md:text-[28px] text-kv-white uppercase kv-shadow-text"
                    >
                        +{scoreAdjustment}
                    </span>
                </button>
                <button
                    onclick={onRemove}
                    class="bg-kv-red border-4 border-black box-border flex items-center justify-center p-2 cursor-pointer hover:opacity-80"
                >
                    <span
                        class="font-kv-body text-xl md:text-[28px] text-kv-white uppercase kv-shadow-text"
                    >
                        -{scoreAdjustment}
                    </span>
                </button>
            </div>
        {/if}
    </div>
</div>
