// ==================================================================================
// PROPERTY SELECTOR COMPONENT
// Displays and allows selection of user properties
// ==================================================================================
const PropertySelector = ({ userProperties, selectedProperty, onPropertyChange, onSettingsClick, canManageProperties, variant }) => {
    const showSettings = canManageProperties && onSettingsClick;
    const headerClass = showSettings ? "flex items-center justify-between mb-2" : "text-center mb-2";
    const isSidebar = variant === 'sidebar';
    const theme = (typeof window !== 'undefined' && window.OTOSidebarTheme) ? window.OTOSidebarTheme : {};
    const cardClass = isSidebar ? (theme.propertyCard || 'bg-black/10 border border-black/20 rounded-xl p-4') : 'bg-primary-dark rounded-lg p-4 border border-primary/30';
    const labelClass = isSidebar ? (theme.propertyLabel || 'text-white/70 text-[10px] font-semibold uppercase tracking-wider') : 'text-white/60 text-[10px] font-semibold uppercase tracking-wider';
    const borderClass = isSidebar ? (theme.propertyBorder || 'border-black/20') : 'border-primary/30';
    const buttonHoverClass = isSidebar ? (theme.propertyButtonHover || 'hover:bg-black/15') : 'hover:bg-primary/50';
    const dashedBorderClass = isSidebar ? (theme.propertyDashedBorder || 'border-2 border-dashed border-black/20 rounded-lg') : 'border-2 border-dashed border-primary/30 rounded-lg';
    const selectClass = isSidebar ? (theme.propertySelect || 'w-full bg-black/15 border border-black/25 text-white rounded-lg px-3 py-2.5 pr-8 text-sm font-semibold focus:outline-none focus:ring-2 focus:ring-black/20 cursor-pointer appearance-none transition-all hover:border-black/40') : 'w-full bg-primary border border-primary/50 text-white rounded-lg px-3 py-2.5 pr-8 text-sm font-semibold focus:outline-none focus:border-white/30 focus:ring-1 focus:ring-white/20 cursor-pointer appearance-none transition-all hover:border-primary/70';

    // Case 1: No properties assigned
    if (!userProperties || userProperties.length === 0) {
        return (
            <div className={cardClass}>
                <div className={headerClass}>
                    <div className={labelClass}>Selected Property</div>
                    {showSettings && (
                        <button
                            onClick={onSettingsClick}
                            className={"p-1.5 rounded-md transition-colors " + buttonHoverClass}
                            title="Property Settings"
                        >
                            <svg className="w-4 h-4 text-white/60 hover:text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                            </svg>
                        </button>
                    )}
                </div>
                <div className={`text-center py-6 border-2 border-dashed rounded-lg ${isSidebar ? 'border-white/20' : 'border-primary/30'}`}>
                    <svg className="w-8 h-8 text-white/40 mx-auto mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
                    </svg>
                    <div className="text-xs text-white/60">No property access</div>
                </div>
            </div>
        );
    }

    // Case 2: Single property - compact static display
    if (userProperties.length === 1) {
        const property = userProperties[0];
        return (
            <div className={cardClass}>
                <div className={showSettings ? "flex items-center justify-between mb-3" : "text-center mb-3"}>
                    <div className={labelClass}>Selected Property</div>
                    {showSettings && (
                        <button
                            onClick={onSettingsClick}
                            className={`p-1.5 rounded-md transition-colors ${buttonHoverClass}`}
                            title="Property Settings"
                        >
                            <svg className="w-4 h-4 text-white/60 hover:text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                            </svg>
                        </button>
                    )}
                </div>
                <div className={showSettings ? "space-y-1" : "space-y-1 text-center"}>
                    <div className="text-white font-bold text-base">{property.property_code}</div>
                    <div className="text-white/80 text-xs leading-snug">{property.name}</div>
                    <div className="text-white/50 text-[11px] leading-tight pt-1 space-y-0.5">
                        {property.address && <div>{property.address}</div>}
                        <div>{property.city}, {property.state} {property.zip || ''}</div>
                    </div>
                </div>
            </div>
        );
    }

    // Case 3: Multiple properties - compact dropdown
    const sortedProperties = [...userProperties].sort((a, b) => {
        if (a.property_code === 'OTO') return -1;
        if (b.property_code === 'OTO') return 1;
        return a.property_code.localeCompare(b.property_code);
    });

    return (
        <div className={cardClass}>
            <div className={showSettings ? "flex items-center justify-between mb-3" : "text-center mb-3"}>
                <div className={labelClass}>Selected Property</div>
                {showSettings && (
                    <button
                        onClick={onSettingsClick}
                        className={`p-1.5 rounded-md transition-colors ${buttonHoverClass}`}
                        title="Property Settings"
                    >
                        <svg className="w-4 h-4 text-white/60 hover:text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                        </svg>
                    </button>
                )}
            </div>

            <div className="relative">
                <select
                    value={selectedProperty?.property_code || ''}
                    onChange={(e) => {
                        const property = userProperties.find(p => p.property_code === e.target.value);
                        onPropertyChange(property);
                    }}
                    className={selectClass}
                    style={{
                        backgroundImage: `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='white'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E")`,
                        backgroundRepeat: 'no-repeat',
                        backgroundPosition: 'right 0.5rem center',
                        backgroundSize: '1.25em 1.25em'
                    }}
                >
                    {sortedProperties.map(property => (
                        <option key={property.property_code} value={property.property_code} className="bg-primary-dark">
                            {property.property_code} - {property.name}
                        </option>
                    ))}
                </select>
            </div>

            {selectedProperty && (
                <div className={`mt-3 pt-3 border-t ${borderClass}`}>
                    <div className="text-white/80 text-xs leading-snug mb-1">{selectedProperty.name}</div>
                    <div className="text-white/50 text-[11px] leading-tight space-y-0.5">
                        {selectedProperty.address && <div>{selectedProperty.address}</div>}
                        <div>{selectedProperty.city}, {selectedProperty.state} {selectedProperty.zip || ''}</div>
                    </div>
                </div>
            )}
        </div>
    );
};

if (typeof window !== 'undefined') window.PropertySelector = PropertySelector;
