'use client';
import React, {ChangeEvent} from 'react';
import {LocationProps, Element} from '@/hooks/settings/useLocations';
import InputField from '@/components/form/InputField';
import TextInput from '@/components/form/TextInput';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import {faChevronRight, faMapMarkerAlt, faPlus} from '@fortawesome/free-solid-svg-icons';
import {useTranslations} from 'next-intl';

interface LocationSettingsListProps {
    sections: LocationProps[];
    newSectionName: string;
    onSectionClick: (sectionIndex: number) => void;
    onAddSection: () => Promise<void>;
    onNewSectionNameChange: (name: string) => void;
}

/**
 * LocationSettingsList - Liste des sections de lieux pour BookSetting/SerieSetting
 * Inclut recherche et bouton d'ajout
 * PAS de scroll interne (géré par parent)
 */
export default function LocationSettingsList({
    sections,
    newSectionName,
    onSectionClick,
    onAddSection,
    onNewSectionNameChange,
}: LocationSettingsListProps): React.JSX.Element {
    const t = useTranslations();

    function countTotalElements(section: LocationProps): number {
        let count: number = section.elements.length;
        section.elements.forEach(function (element: Element): void {
            count += element.subElements.length;
        });
        return count;
    }

    return (
        <div className="space-y-4">
            <div className="bg-tertiary/90 backdrop-blur-sm rounded-xl shadow-lg p-4 border border-secondary/50">
                <InputField
                    input={
                        <TextInput
                            value={newSectionName}
                            setValue={function (e: ChangeEvent<HTMLInputElement>): void {
                                onNewSectionNameChange(e.target.value);
                            }}
                            placeholder={t("locationComponent.newSectionPlaceholder")}
                        />
                    }
                    actionIcon={faPlus}
                    actionLabel={t("locationComponent.addSectionLabel")}
                    addButtonCallBack={onAddSection}
                />
            </div>

            <div className="space-y-2 px-2">
                {sections.length === 0 ? (
                    <div className="flex flex-col items-center justify-center py-12 text-center">
                        <div className="w-20 h-20 rounded-full bg-primary/10 flex items-center justify-center mb-4">
                            <FontAwesomeIcon icon={faMapMarkerAlt} className="text-primary w-10 h-10"/>
                        </div>
                        <h3 className="text-text-primary font-semibold text-lg mb-2">
                            {t("locationComponent.noSectionAvailable")}
                        </h3>
                        <p className="text-muted text-sm max-w-xs">
                            {t("locationComponent.noSectionDescription")}
                        </p>
                    </div>
                ) : (
                    sections.map(function (section: LocationProps, index: number): React.JSX.Element {
                        return (
                            <div
                                key={section.id}
                                onClick={function (): void { onSectionClick(index); }}
                                className="group flex items-center p-4 bg-secondary/30 rounded-xl border-l-4 border-primary border border-secondary/50 cursor-pointer hover:bg-secondary hover:shadow-md hover:scale-102 transition-all duration-200 hover:border-primary/50"
                            >
                                <div className="w-12 h-12 rounded-full border-2 border-primary overflow-hidden bg-secondary shadow-md group-hover:scale-110 transition-transform flex items-center justify-center">
                                    <FontAwesomeIcon icon={faMapMarkerAlt} className="text-primary w-6 h-6"/>
                                </div>

                                <div className="ml-4 flex-1 min-w-0">
                                    <div className="text-text-primary font-bold text-base group-hover:text-primary transition-colors">
                                        {section.name}
                                    </div>
                                    <div className="text-text-secondary text-sm mt-0.5">
                                        {t("locationComponent.elementsCount", {count: countTotalElements(section)})}
                                    </div>
                                </div>

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