'use client';
import React, {useState} from 'react';
import {CharacterProps} from '@/lib/models/Character';
import InputField from '@/components/form/InputField';
import TextInput from '@/components/form/TextInput';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import {faChevronRight, faPlus, faUser} from '@fortawesome/free-solid-svg-icons';
import {useTranslations} from 'next-intl';

interface CharacterEditorListProps {
    characters: CharacterProps[];
    onCharacterClick: (character: CharacterProps) => void;
    onAddCharacter: () => void;
}

/**
 * CharacterEditorList - Liste des personnages pour ComposerRightBar
 * Version compacte sans groupage par catégorie
 * PAS de scroll interne (géré par parent ComposerRightBar)
 */
export default function CharacterEditorList({
    characters,
    onCharacterClick,
    onAddCharacter,
}: CharacterEditorListProps): React.JSX.Element {
    const t = useTranslations();
    const [searchQuery, setSearchQuery] = useState<string>('');

    function getFilteredCharacters(): CharacterProps[] {
        return characters.filter(function (char: CharacterProps): boolean {
            return char.name.toLowerCase().includes(searchQuery.toLowerCase()) ||
                (char.lastName?.toLowerCase().includes(searchQuery.toLowerCase()) ?? false);
        });
    }

    const filteredCharacters: CharacterProps[] = getFilteredCharacters();

    return (
        <div className="space-y-3">
            <div className="px-2">
                <InputField
                    input={
                        <TextInput
                            value={searchQuery}
                            setValue={function (e: React.ChangeEvent<HTMLInputElement>): void {
                                setSearchQuery(e.target.value);
                            }}
                            placeholder={t('characterList.search')}
                        />
                    }
                    actionIcon={faPlus}
                    actionLabel={t('characterList.add')}
                    addButtonCallBack={async function (): Promise<void> {
                        onAddCharacter();
                    }}
                />
            </div>

            <div className="px-2 space-y-2">
                {filteredCharacters.length === 0 ? (
                    <div className="flex flex-col items-center justify-center py-8 text-center">
                        <div className="w-16 h-16 rounded-full bg-primary/10 flex items-center justify-center mb-3">
                            <FontAwesomeIcon icon={faUser} className="text-primary w-8 h-8"/>
                        </div>
                        <h3 className="text-text-primary font-semibold text-base mb-1">
                            {t('characterList.noCharacters')}
                        </h3>
                        <p className="text-muted text-sm max-w-xs">
                            {t('characterList.noCharactersDescription')}
                        </p>
                    </div>
                ) : (
                    filteredCharacters.map(function (char: CharacterProps): React.JSX.Element {
                        return (
                            <div
                                key={char.id}
                                onClick={function (): void { onCharacterClick(char); }}
                                className="group flex items-center p-3 bg-secondary/30 rounded-lg border-l-4 border-primary border border-secondary/50 cursor-pointer hover:bg-secondary hover:shadow-md transition-all duration-200 hover:border-primary/50"
                            >
                                <div className="w-10 h-10 rounded-full border-2 border-primary overflow-hidden bg-secondary shadow-sm group-hover:scale-110 transition-transform">
                                    {char.image ? (
                                        <img
                                            src={char.image}
                                            alt={char.name}
                                            className="w-full h-full object-cover"
                                        />
                                    ) : (
                                        <div className="w-full h-full flex items-center justify-center bg-primary/10 text-primary font-bold text-sm">
                                            {char.name?.charAt(0)?.toUpperCase() || '?'}
                                        </div>
                                    )}
                                </div>

                                <div className="ml-3 flex-1 min-w-0">
                                    <div className="text-text-primary font-semibold text-sm group-hover:text-primary transition-colors truncate">
                                        {char.name || t('characterList.unknown')}
                                    </div>
                                    <div className="text-muted text-xs truncate">
                                        {char.title || char.role || t('characterList.noRole')}
                                    </div>
                                </div>

                                <div className="w-6 flex justify-center">
                                    <FontAwesomeIcon
                                        icon={faChevronRight}
                                        className="text-muted group-hover:text-primary group-hover:translate-x-1 transition-all w-3 h-3"
                                    />
                                </div>
                            </div>
                        );
                    })
                )}
            </div>
        </div>
    );
}
