package com.manalejandro.odb2bluetooth.data.repository

import com.manalejandro.odb2bluetooth.data.database.dao.DtcCodeDao
import com.manalejandro.odb2bluetooth.data.database.dao.ObdSignalDao
import com.manalejandro.odb2bluetooth.data.database.dao.VehicleDao
import com.manalejandro.odb2bluetooth.data.database.dto.SignalDto
import com.manalejandro.odb2bluetooth.data.database.dto.VehicleDto
import com.manalejandro.odb2bluetooth.data.database.entities.Brand
import com.manalejandro.odb2bluetooth.data.database.entities.DtcCode
import com.manalejandro.odb2bluetooth.data.database.entities.ObdSignal
import kotlinx.coroutines.flow.Flow

/**
 * Repository for OBD database operations
 */
class ObdRepository(
    private val vehicleDao: VehicleDao,
    private val obdSignalDao: ObdSignalDao,
    private val dtcCodeDao: DtcCodeDao
) {

    // Vehicle operations
    fun getAllVehicles(): Flow<List<VehicleDto>> = vehicleDao.getAllVehicles()

    fun searchVehicles(query: String): Flow<List<VehicleDto>> = vehicleDao.searchVehicles(query)

    fun getAllBrands(): Flow<List<Brand>> = vehicleDao.getAllBrands()

    // Signal operations
    fun getSignalsByModel(modelId: Int): Flow<List<SignalDto>> =
        obdSignalDao.getSignalsDtoByModel(modelId)

    suspend fun getSignalById(signalId: Int): ObdSignal? =
        obdSignalDao.getSignalById(signalId)

    fun searchSignals(modelId: Int, query: String): Flow<List<ObdSignal>> =
        obdSignalDao.searchSignals(modelId, query)

    // DTC operations
    fun getAllDtcCodes(): Flow<List<DtcCode>> = dtcCodeDao.getAllDtcCodes()

    suspend fun getDtcCodeByCode(code: String): DtcCode? =
        dtcCodeDao.getDtcCodeByCode(code)

    fun searchDtcCodes(query: String): Flow<List<DtcCode>> =
        dtcCodeDao.searchDtcCodes(query)
}

