import clsx from "clsx";
import { Sidebar } from "./sidebar";
import { SettingsModal } from "./settings-modal";
import { useAppState } from "../contexts/state";
import { useBool } from "@common/hooks/useBool";
import type { Story } from "../contexts/state";
import styles from '../assets/menu-sidebar.module.css';
import { useState } from "preact/hooks";

// ─── Story Item ───────────────────────────────────────────────────────────────

interface StoryItemProps {
    story: Story;
    active: boolean;
    onSelect: () => void;
    onRename: (newTitle: string) => void;
    onDelete: () => void;
}

const StoryItem = ({ story, active, onSelect, onRename, onDelete }: StoryItemProps) => {
    const [isEditing, setIsEditing] = useState(false);
    const [editTitle, setEditTitle] = useState(story.title);

    const handleSubmit = () => {
        if (editTitle.trim()) {
            onRename(editTitle.trim());
        }
        setIsEditing(false);
    };

    const handleKeyDown = (e: KeyboardEvent) => {
        if (e.key === 'Enter') {
            handleSubmit();
        } else if (e.key === 'Escape') {
            setEditTitle(story.title);
            setIsEditing(false);
        }
    };

    const handleBlur = () => {
        handleSubmit();
    };

    if (isEditing) {
        return (
            <div class={clsx(styles.itemWrapper, active && styles.active)}>
                <input
                    class={styles.input}
                    value={editTitle}
                    onInput={(e) => setEditTitle((e.target as HTMLInputElement).value)}
                    onKeyDown={handleKeyDown}
                    onBlur={handleBlur}
                    autoFocus
                />
            </div>
        );
    }

    return (
        <div class={clsx(styles.itemWrapper, active && styles.active)}>
            <button
                class={clsx(styles.item, active && styles.active)}
                onClick={onSelect}
            >
                {story.title}
            </button>
            <div class={styles.actions}>
                <button class={styles.actionButton} onClick={() => setIsEditing(true)} title="Rename">
                    ✎
                </button>
                <button class={styles.actionButton} onClick={onDelete} title="Delete">
                    ×
                </button>
            </div>
        </div>
    );
};

// ─── Menu Sidebar ─────────────────────────────────────────────────────────────

export const MenuSidebar = () => {
    const { stories, currentStory, dispatch } = useAppState();
    const isSettingsOpen = useBool(false);

    const handleCreate = () => {
        dispatch({ type: 'CREATE_STORY', title: 'New Story' });
    };

    const handleSelect = (id: string) => {
        dispatch({ type: 'SELECT_STORY', id });
    };

    const handleRename = (id: string, newTitle: string) => {
        dispatch({ type: 'RENAME_STORY', id, title: newTitle });
    };

    const handleDelete = (id: string) => {
        const story = stories.find(s => s.id === id);
        if (!story) return;
        if (confirm(`Delete "${story.title}"?`)) {
            dispatch({ type: 'DELETE_STORY', id });
        }
    };

    return (
        <Sidebar side="left">
            <div class={styles.menu}>
                <button class={styles.newButton} onClick={handleCreate}>
                    + New Story
                </button>
                <div class={styles.list}>
                    {stories.map(story => (
                        <StoryItem
                            key={story.id}
                            story={story}
                            active={story.id === currentStory?.id}
                            onSelect={() => handleSelect(story.id)}
                            onRename={(newTitle) => handleRename(story.id, newTitle)}
                            onDelete={() => handleDelete(story.id)}
                        />
                    ))}
                </div>
                <button class={styles.settingsButton} onClick={isSettingsOpen.toggle}>
                    ⚙ Settings
                </button>
            </div>
            {isSettingsOpen.value && (
                <SettingsModal onClose={isSettingsOpen.toggle} />
            )}
        </Sidebar>
    );
};
