package com.swoosh.microblog.ui.pages

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.swoosh.microblog.data.MobiledocBuilder
import com.swoosh.microblog.data.model.GhostPage
import com.swoosh.microblog.data.repository.PageRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class PagesViewModel(application: Application) : AndroidViewModel(application) {

    private val repository = PageRepository(application)

    private val _uiState = MutableStateFlow(PagesUiState())
    val uiState: StateFlow<PagesUiState> = _uiState.asStateFlow()

    init {
        loadPages()
    }

    fun loadPages() {
        viewModelScope.launch {
            _uiState.update { it.copy(isLoading = true, error = null) }
            repository.fetchPages().fold(
                onSuccess = { pages ->
                    _uiState.update { it.copy(pages = pages, isLoading = false) }
                },
                onFailure = { e ->
                    _uiState.update { it.copy(isLoading = false, error = e.message) }
                }
            )
        }
    }

    fun savePage(title: String, content: String, slug: String?, status: String) {
        viewModelScope.launch {
            _uiState.update { it.copy(isLoading = true, error = null) }
            val mobiledoc = MobiledocBuilder.build(content)
            val page = GhostPage(
                title = title,
                mobiledoc = mobiledoc,
                slug = slug?.takeIf { it.isNotBlank() },
                status = status
            )
            repository.createPage(page).fold(
                onSuccess = {
                    _uiState.update { it.copy(isEditing = false, isCreating = false, editingPage = null) }
                    loadPages()
                },
                onFailure = { e ->
                    _uiState.update { it.copy(isLoading = false, error = e.message) }
                }
            )
        }
    }

    fun updatePage(id: String, page: GhostPage) {
        viewModelScope.launch {
            _uiState.update { it.copy(isLoading = true, error = null) }
            repository.updatePage(id, page).fold(
                onSuccess = {
                    _uiState.update { it.copy(isEditing = false, isCreating = false, editingPage = null) }
                    loadPages()
                },
                onFailure = { e ->
                    _uiState.update { it.copy(isLoading = false, error = e.message) }
                }
            )
        }
    }

    fun deletePage(id: String) {
        viewModelScope.launch {
            _uiState.update { it.copy(isLoading = true, error = null) }
            repository.deletePage(id).fold(
                onSuccess = { loadPages() },
                onFailure = { e ->
                    _uiState.update { it.copy(isLoading = false, error = e.message) }
                }
            )
        }
    }

    fun startEditing(page: GhostPage) {
        _uiState.update { it.copy(editingPage = page, isEditing = true, isCreating = false) }
    }

    fun startCreating() {
        _uiState.update { it.copy(editingPage = null, isEditing = false, isCreating = true) }
    }

    fun cancelEditing() {
        _uiState.update { it.copy(editingPage = null, isEditing = false, isCreating = false) }
    }

    fun getBlogUrl(): String? = repository.getBlogUrl()
}

data class PagesUiState(
    val pages: List<GhostPage> = emptyList(),
    val isLoading: Boolean = false,
    val error: String? = null,
    val editingPage: GhostPage? = null,
    val isEditing: Boolean = false,
    val isCreating: Boolean = false
)
