import { apiRequest } from '../lib/api'
import type { AuthUser, MeResponse } from '../types/auth'
import type { ProfileFormData } from '../types/profile'

export async function updateProfile(data: ProfileFormData) {
  return apiRequest<{ message: string; user: AuthUser }>('/auth/profile', {
    method: 'PATCH',
    auth: true,
    body: {
      firstName: data.firstName,
      lastName: data.lastName,
      firstNameEn: data.firstNameEn,
      lastNameEn: data.lastNameEn,
      email: data.email || null,
      about: data.about,
      city: data.city,
      address: data.address,
      landline: data.landline,
      postalCode: data.postalCode,
      instagram: data.instagram,
      telegramId: data.telegramId,
      linkedin: data.linkedin,
    },
  })
}

export async function changePassword(currentPassword: string, newPassword: string) {
  return apiRequest<{ message: string }>('/auth/change-password', {
    method: 'POST',
    auth: true,
    body: { currentPassword, newPassword },
  })
}

export async function fetchProfile() {
  return apiRequest<MeResponse>('/auth/me', { auth: true })
}
