import { useEffect, useState } from 'react';

import { useFetchData } from './useFetchData';

export interface RoleOption {
  label: string;
  value: string;
}

export const useRoles = () => {
  const { data: rolesData, loading } = useFetchData(
    'role/findMany',
    'post',
    {},
  );
  const [roleOptions, setRoleOptions] = useState<RoleOption[]>([]);

  useEffect(() => {
    if (rolesData?.data) {
      const roles = Array.isArray(rolesData.data) ? rolesData.data : [];
      const options = roles.map((role: any) => ({
        label: `${role.name} (${role.description || ''})`,
        value: role.name,
      }));
      setRoleOptions(options);
    }
  }, [rolesData]);

  return {
    roleOptions,
    loading: loading || !rolesData,
    rolesData,
  };
};
