package com.devlab.app.data.repository

import com.devlab.app.data.api.RetrofitClient
import com.devlab.app.data.model.ProfileUpdateRequest

class ProfileRepository {

    private val api get() = RetrofitClient.getApiService()

    suspend fun getProfile(): Result<com.devlab.app.data.model.UserProfile> {
        return try {
            val response = api.getProfile()
            if (response.success && response.data != null) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.error ?: "Failed to load profile"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun updateProfile(
        email: String,
        currentPassword: String?,
        newPassword: String?,
        confirmPassword: String?
    ): Result<String> {
        return try {
            val request = ProfileUpdateRequest(
                email = email,
                current_password = currentPassword?.takeIf { it.isNotBlank() },
                new_password = newPassword?.takeIf { it.isNotBlank() },
                confirm_password = confirmPassword?.takeIf { it.isNotBlank() }
            )
            val response = api.updateProfile(request)
            if (response.success) {
                Result.success(response.data?.get("message") ?: "Profile updated")
            } else {
                Result.failure(Exception(response.error ?: "Update failed"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
}
