package com.example.util.simpletimetracker.domain.interactor

import com.example.util.simpletimetracker.domain.model.Category
import com.example.util.simpletimetracker.domain.repo.CategoryRepo
import com.example.util.simpletimetracker.domain.repo.RecordTypeCategoryRepo
import com.example.util.simpletimetracker.domain.repo.RecordTypeGoalRepo
import java.util.Locale
import javax.inject.Inject

class CategoryInteractor @Inject constructor(
    private val categoryRepo: CategoryRepo,
    private val recordTypeCategoryRepo: RecordTypeCategoryRepo,
    private val recordTypeGoalRepo: RecordTypeGoalRepo,
) {

    suspend fun getAll(): List<Category> {
        return categoryRepo.getAll()
            .sortedBy { it.name.lowercase(Locale.getDefault()) }
    }

    suspend fun get(id: Long): Category? {
        return categoryRepo.get(id)
    }

    suspend fun add(category: Category): Long {
        return categoryRepo.add(category)
    }

    suspend fun remove(id: Long) {
        recordTypeCategoryRepo.removeAll(id)
        recordTypeGoalRepo.removeByCategory(id)
        categoryRepo.remove(id)
    }
}