package com.example.shawarma

import android.app.Application
import androidx.room.Room
import com.example.shawarma.data.api.MyServerService
import com.example.shawarma.data.api.repos.RestProductRepository
import com.example.shawarma.data.api.repos.RestUserRepository
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.repos.OrderProductRepository
import com.example.shawarma.data.repos.OrderRepository
import com.example.shawarma.data.repos.ProductRepository
import com.example.shawarma.data.repos.UserRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    @Provides
    @Singleton
    fun provideDatabase(app: Application) :  AppDatabase {
        return Room.databaseBuilder(
            app,
            AppDatabase::class.java,
            AppDatabase.DB_NAME
        ).build()
    }

    @Provides
    @Singleton
    fun provideServerService() : MyServerService {
        return MyServerService.getInstance()
    }

    @Provides
    @Singleton
    fun provideUserRepository(db: AppDatabase) : UserRepository {
        return UserRepository(db.userDao())
    }

    @Provides
    @Singleton
    fun provideRestUserRepository(service: MyServerService) : RestUserRepository {
        return RestUserRepository(service)
    }

    @Provides
    @Singleton
    fun provideProductRepository(db: AppDatabase) : ProductRepository {
        return ProductRepository(db.productDao(), db.orderProductDao())
    }

    @Provides
    @Singleton
    fun provideRestProductRepository(service: MyServerService) : RestProductRepository {
        return RestProductRepository(service)
    }

    @Provides
    @Singleton
    fun provideOrderRepository(db: AppDatabase) : OrderRepository {
        return OrderRepository(db.orderDao())
    }

    @Provides
    @Singleton
    fun provideOrderProductRepository(db: AppDatabase) : OrderProductRepository {
        return OrderProductRepository(db.orderProductDao())
    }


}