/* eslint-disable @typescript-eslint/no-explicit-any */
import PocketBase from 'pocketbase';

const pb = new PocketBase(import.meta.env.VITE_BASE_URL_API);

export interface RegisterResponse {
  code: number;
  message: string;
  data: any;
}
export const register = async (
  email: string,
  password: string,
  name: string
): Promise<RegisterResponse> => {
  const collection = 'users';
  try {
    const response = await pb.collection(collection).create({
      email: email,
      password: password,
      passwordConfirm: password,
      name: name,
      role: ['user'],
    });

    return {
      code: 200,
      message: 'Registro exitoso',
      data: response,
    };
  } catch (error) {
    console.error('Error al registrar:', error);
    const errorResponse = error as {
      status?: number;
      message?: string;
      data?: any;
    };

    return {
      code: errorResponse.status || 400,
      message: errorResponse.message || 'Error desconocido',
      data: errorResponse.data || {},
    };
  }
};
