/*
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package com.runicgateway.app.ui.player

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.runicgateway.app.data.api.dto.PlayerHouseDto
import com.runicgateway.app.data.repository.PlayerShardRepository
import com.runicgateway.app.ui.UiState
import com.runicgateway.app.ui.toUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * The player's OWN houses with decay/IDOC status (PLAN.md §6.3), read from
 * `/player/shard/houses` — never anyone else's. A `503` renders as offline/retry (§7).
 */
@HiltViewModel
class MyHousesViewModel @Inject constructor(
    private val repository: PlayerShardRepository,
) : ViewModel() {

    private val _state = MutableStateFlow<UiState<List<PlayerHouseDto>>>(UiState.Loading)
    val state: StateFlow<UiState<List<PlayerHouseDto>>> = _state.asStateFlow()

    init {
        load()
    }

    fun load() {
        _state.value = UiState.Loading
        viewModelScope.launch {
            _state.value = repository.houses().toUiState()
        }
    }
}
