// lib/auth.ts
import { apiFetch } from './api-client'

// Type for registration data
interface RegisterData {
  email: string
  phone: string
  password: string
  password_confirmation: string
  firstName: string
  lastName: string
  role?: string
  [key: string]: string | number | boolean | undefined // More specific types for additional properties
}

export interface AuthUser {
  id: number
  firstName: string
  lastName: string
  email: string
  phone: string
  role: string
}

export interface LoginResponse {
  token: { token: string; [key: string]: unknown }
  user: AuthUser
}

export interface CurrentUserResponse {
  user: AuthUser
}

export const login = async (identifier: string, password: string): Promise<LoginResponse> => {
  return apiFetch<LoginResponse>('/auth/login', {
    method: 'POST',
    body: { identifier, password, token_name: 'frontend' },
  })
}

export const register = async (data: RegisterData) => {
  return apiFetch('/auth/register', { method: 'POST', body: data })
}

export const getCurrentUser = async (): Promise<CurrentUserResponse> => {
  return apiFetch<CurrentUserResponse>('/auth/me')
}

export const logout = async () => {
  return apiFetch('/auth/logout', { method: 'POST' })
}
