import { apiClient, API_URL } from './apiClient'

export interface LoginResponse {
    access_token: string
    token_type: string
    role: string
    full_name: string
    profile_photo_url?: string
}

export const authService = {
    async login(params: { email: string; password: string; keep_session?: boolean }): Promise<LoginResponse> {
        const response = await apiClient.post<LoginResponse>('/api/auth/login', params)
        return response.data
    },

    async googleLogin(idToken: string): Promise<LoginResponse> {
        const response = await apiClient.post<LoginResponse>('/api/auth/google', { id_token: idToken })
        return response.data
    },

    async registerPassenger(data: any) {
        const response = await apiClient.post('/api/auth/register/passenger', data)
        return response.data
    },

    async registerDriver(formData: FormData) {
        const response = await apiClient.post('/api/auth/register/driver', formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
        return response.data
    },

    async getCurrentUser() {
        const response = await apiClient.get('/api/auth/me')
        return response.data
    },

    async updateMe(formData: FormData) {
        const response = await apiClient.patch('/api/auth/me', formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
        return response.data
    },

    logout() {
        localStorage.removeItem('auth_token')
        localStorage.removeItem('user_role')
        localStorage.removeItem('user_name')
        localStorage.removeItem('profile_photo_url')
    },

    getApiUrl() {
        return API_URL
    }
}
