import React from 'react';
import { Control, UseFormClearErrors } from 'react-hook-form';
import { FormField } from '@/components/ui/form';
import { STUDENT_CONDITION_LIST } from '@/constants';
import { SearchableSelect } from '../../forms/SearchableSelect';
import { CURRENCY_LIST } from '@/constants';

interface StudentConditionCurrencyFieldsProps {
  control: Control<any>;
  clearErrors: UseFormClearErrors<any>;
  studentCondition: string;
  currency: string;
  dropdownStates: {
    studentCondition: boolean;
    currency: boolean;
  };
  toggleDropdown: (key: string) => void;
  handleStudentConditionChange: (value: string) => void;
  setCurrency: (value: string) => void;
  setDropdownStates: React.Dispatch<
    React.SetStateAction<{
      studentCondition: boolean;
      currency: boolean;
      // include other dropdown states if needed
    }>
  >;
}

const StudentConditionCurrencyFields: React.FC<
  StudentConditionCurrencyFieldsProps
> = ({
  control,
  clearErrors,
  studentCondition,
  currency,
  dropdownStates,
  toggleDropdown,
  handleStudentConditionChange,
  setCurrency,
  setDropdownStates,
}) => {
  return (
    <>
      {/* Student Condition Field */}
      <FormField
        control={control}
        name="student_condition"
        render={({ field }) => (
          <SearchableSelect
            field={field}
            label="Student Condition"
            placeholder="Choose Student Condition"
            defaultValue={studentCondition || field.value}
            options={STUDENT_CONDITION_LIST.map((item) => ({
              id: String(item.id),
              name: item.name,
            }))}
            isOpen={dropdownStates.studentCondition}
            onOpenChange={() => toggleDropdown('studentCondition')}
            onSelect={(value) => {
              field.onChange(String(value));
              clearErrors('student_condition');
              setDropdownStates((prev) => ({
                ...prev,
                studentCondition: false,
              }));
              handleStudentConditionChange(String(value));
            }}
            required
          />
        )}
      />

      {/* Currency Field */}
      <FormField
        control={control}
        name="currency"
        render={({ field }) => (
          <SearchableSelect
            field={field}
            label="Currency"
            placeholder="Choose Currency"
            defaultValue={currency || field.value}
            options={CURRENCY_LIST.map((item) => ({
              id: item.id,
              name: item.name,
            }))}
            isOpen={dropdownStates.currency}
            onOpenChange={() => toggleDropdown('currency')}
            onSelect={(value) => {
              field.onChange(value);
              clearErrors('currency');
              setDropdownStates((prev) => ({
                ...prev,
                currency: false,
              }));
              setCurrency(String(value));
            }}
            required
          />
        )}
      />
    </>
  );
};

export default StudentConditionCurrencyFields;
