import { Controller, Control, FieldError } from "react-hook-form";
import Select from "@/src/components/admin/form/Select";

interface FormSelectProps {
  name: string;
  control: Control<any>;
  label: string;
  options: { label: string; value: string }[];
  placeholder?: string;
  required?: boolean;
  disabled?: boolean;
  error?: FieldError;
  onChange?: (value: string) => void;
}

export default function FormSelect({
  name,
  control,
  label,
  options,
  placeholder,
  required,
  disabled,
  error,
  onChange,
}: FormSelectProps) {
  return (
    <div>
      <label className="text-sm font-medium text-gray-700 mb-1 block">
        {label}
        {required && <span className="text-red-500">*</span>}
      </label>
      <Controller
        name={name}
        control={control}
        render={({ field }) => (
          <Select
            options={options}
            placeholder={placeholder}
            value={field.value}
            onChange={(val) => {
              field.onChange(val);
              onChange?.(String(val || ""));
            }}
            disabled={disabled}
            error={!!error}
            errorMessage={error?.message}
          />
        )}
      />
    </div>
  );
}
