import { useState, useEffect } from 'react';
import { sessionStore } from '../store/sessionStore';

const findingsCategories = [
    {
        name: 'Thin Acetowhite Epithelium',
        features: ['Dull white', 'Appears slowly', 'Fades quickly']
    },
    {
        name: 'Borders',
        features: ['Irregular', 'Geographic', 'Feathered edges', 'Sharp', 'Raised', 'Rolled edges', 'Inner border sign']
    },
    {
        name: 'Vascular Pattern',
        features: ['Fine punctation', 'Fine mosaic', 'Coarse punctation', 'Coarse mosaic']
    },
    {
        name: 'Dense Acetowhite Epithelium',
        features: ['Chalky white', 'Oyster white', 'Greyish white', 'Rapid onset', 'Persists']
    },
    {
        name: 'Gland Openings',
        features: ['Cuffed', 'Enlarged crypt openings']
    },
    {
        name: 'Non-Specific Abnormal Findings',
        features: ['Leukoplakia (keratosis)', 'Hyperkeratosis', 'Erosion']
    }
];

export function AceticFindingsForm() {
    // Restore previously saved findings from sessionStore
    const savedAcetic = sessionStore.get().aceticFindings ?? {};

    const [selectedCategories, setSelectedCategories] = useState<Record<string, boolean>>(
        savedAcetic.selectedCategories ?? {}
    );
    const [selectedFindings, setSelectedFindings] = useState<Record<string, boolean>>(
        savedAcetic.selectedFindings ?? {}
    );
    const [additionalNotes, setAdditionalNotes] = useState<string>(
        savedAcetic.additionalNotes ?? ''
    );

    // Persist to sessionStore whenever selections change
    useEffect(() => {
        sessionStore.merge({
            aceticFindings: { selectedCategories, selectedFindings, additionalNotes }
        });
    }, [selectedCategories, selectedFindings, additionalNotes]);


    const toggleCategory = (name: string) =>
        setSelectedCategories(prev => ({ ...prev, [name]: !prev[name] }));

    const toggleFinding = (label: string) =>
        setSelectedFindings(prev => ({ ...prev, [label]: !prev[label] }));

    return (
        <div className="bg-gradient-to-b from-white to-blue-50 border-2 border-[#05998c] rounded-xl shadow-lg p-5 md:p-6">
            {/* Header */}
            <div className="mb-6 pb-5 border-b-2 border-[#05998c]">
                <div className="flex items-center gap-2 mb-2">
                    <div className="w-1 h-6 bg-[#05998c] rounded-full" />
                    <p className="text-lg uppercase tracking-wider font-bold text-[#05998c]">Clinical Findings</p>
                </div>
                <p className="text-sm text-gray-600 ml-3">Select findings observed during acetic acid examination</p>
            </div>

            <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-x-8 gap-y-6 text-base text-gray-800">
                {findingsCategories.map((category, index) => (
                    <div key={category.name} className={`space-y-3 ${index < findingsCategories.length - 1 && 'lg:border-r lg:border-gray-300 lg:pr-8'}`}>
                        {/* Category header */}
                        <label className="flex items-center gap-3 cursor-pointer hover:bg-blue-50 p-2 rounded transition-colors -ml-2">
                            <input
                                type="checkbox"
                                checked={!!selectedCategories[category.name]}
                                onChange={() => toggleCategory(category.name)}
                                className="w-5 h-5 cursor-pointer accent-[#05998c] flex-shrink-0"
                            />
                            <div className="flex items-center gap-2 flex-1">
                                <span className="inline-block w-2 h-2 bg-[#05998c] rounded-full flex-shrink-0" />
                                <p className="font-bold text-gray-900 text-sm md:text-base">{category.name}</p>
                            </div>
                        </label>

                        {/* Feature checkboxes */}
                        <div className="space-y-2 pl-8">
                            {category.features.map(feature => (
                                <label key={feature} className="flex items-center gap-2 text-gray-700 hover:text-[#05998c] cursor-pointer transition-colors">
                                    <input
                                        type="checkbox"
                                        checked={!!selectedFindings[feature]}
                                        onChange={() => toggleFinding(feature)}
                                        className="w-4 h-4 rounded border-[#05998c] cursor-pointer accent-[#05998c] flex-shrink-0"
                                    />
                                    <span className="text-sm">{feature}</span>
                                </label>
                            ))}
                        </div>
                    </div>
                ))}
            </div>

            {/* Additional Notes */}
            <div className="mt-6 pt-5 border-t border-gray-300">
                <label className="flex items-center gap-2 mb-3">
                    <span className="inline-block w-2 h-2 bg-[#05998c] rounded-full flex-shrink-0" />
                    <p className="font-bold text-gray-900 text-base">Additional Notes</p>
                </label>
                <textarea
                    value={additionalNotes}
                    onChange={e => setAdditionalNotes(e.target.value)}
                    placeholder="Add any clinical observations from the acetic acid examination..."
                    className="w-full border-2 border-[#05998c] rounded-lg px-4 py-3 text-sm focus:outline-none focus:ring-2 focus:ring-[#05998c] focus:border-transparent bg-white/80 resize-none"
                    rows={4}
                />
            </div>
        </div>
    );
}
