package id.iiyh.inatrading.feature.explore.data.repository

import id.iiyh.inatrading.core.data.remote.ApiService
import id.iiyh.inatrading.feature.explore.domain.LocationPage
import id.iiyh.inatrading.feature.explore.domain.LocationRepository
import javax.inject.Inject

class LocationRepositoryImpl @Inject constructor(
    private val apiService: ApiService,
) : LocationRepository {

    override suspend fun getLocations(page: Int, limit: Int): Result<LocationPage> {
        return try {
            val response = apiService.getLocations(page = page, limit = limit)
            if (response.isSuccess) {
                Result.success(
                    LocationPage(
                        items = response.rows,
                        totalItem = response.totalItem,
                        totalPage = response.totalPage,
                    )
                )
            } else {
                Result.failure(Exception(response.responseDesc ?: "Gagal memuat lokasi"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
}
