// API Response structure from /api/user/me export interface UserProfileResponse { id: number; username: string; email: string; email_verified_at: string | null; updated_at: string; created_at: string; role: { code: string; name: { en: string; th: string; }; }; profile: { prefix: { en: string; th: string; }; first_name: string; last_name: string; avatar_url: string | null; birth_date: string | null; phone: string | null; }; } export interface ApiResponse { code: number; message: string; data: T; } // Request body for PUT /api/user/me export interface UpdateProfileRequest { prefix?: { en: string; th: string; }; first_name: string; last_name: string; phone?: string | null; avatar_url?: string | null; birth_date?: string | null; } // Helper function to get auth token from cookie const getAuthToken = (): string => { const tokenCookie = useCookie('token'); return tokenCookie.value || ''; }; export const userService = { async getProfile(): Promise { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch('/api/user/me', { baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` } }); return response; }, async updateProfile(data: UpdateProfileRequest): Promise> { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch>('/api/user/me', { method: 'PUT', baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` }, body: data }); return response; }, async changePassword(oldPassword: string, newPassword: string): Promise> { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch>('/api/user/change-password', { method: 'POST', baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` }, body: { oldPassword, newPassword } }); return response; }, async uploadAvatar(file: File): Promise> { const config = useRuntimeConfig(); const token = getAuthToken(); const formData = new FormData(); formData.append('file', file); const response = await $fetch>('/api/user/upload-avatar', { method: 'POST', baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` }, body: formData }); return response; } };