import React, { forwardRef } from 'react';
import { ChevronDown } from 'lucide-react';
import { SelectProps } from '../lib/interfaces';

const Select = forwardRef<HTMLSelectElement, SelectProps>(
  ({ label, options, error, placeholder = 'Select an option', onChange, className = '', ...props }, ref) => {
    const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
      onChange?.(e.target.value);
    };

    return (
      <div className="mb-6">
        <label className="block text-gray-800 text-sm font-medium mb-2">
          {label}
        </label>
        <div className="relative">
          <select
            ref={ref}
            onChange={handleChange}
            className={`
              w-full px-4 py-3 rounded-full border border-gray-300 
              focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent
              transition-all duration-200 ease-in-out
              text-gray-700 bg-white appearance-none
              ${error ? 'border-red-500 ring-2 ring-red-200' : ''}
              ${className}
            `}
            {...props}
          >
            <option value="" disabled>
              {placeholder}
            </option>
            {options.map((option) => (
              <option key={option.value} value={option.value}>
                {option.label}
              </option>
            ))}
          </select>
          <div className="absolute inset-y-0 right-0 flex items-center px-3 pointer-events-none">
            <ChevronDown className="w-5 h-5 text-gray-400" />
          </div>
        </div>
        {error && (
          <p className="mt-2 text-sm text-red-600">{error}</p>
        )}
      </div>
    );
  }
);

Select.displayName = 'Select';

export default Select;