package com.placeholder.sherpai2.di

import android.content.Context
import androidx.room.Room
import com.placeholder.sherpai2.data.local.AppDatabase
import com.placeholder.sherpai2.data.local.MIGRATION_7_8
import com.placeholder.sherpai2.data.local.MIGRATION_8_9
import com.placeholder.sherpai2.data.local.MIGRATION_9_10
import com.placeholder.sherpai2.data.local.dao.*
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

/**
 * DatabaseModule - Provides database and ALL DAOs
 *
 * VERSION 10 UPDATES:
 * - Added UserFeedbackDao for cluster refinement
 * - Added MIGRATION_9_10
 *
 * VERSION 9 UPDATES:
 * - Added FaceCacheDao for per-face metadata
 * - Added MIGRATION_8_9
 *
 * PHASE 2 UPDATES:
 * - Added PersonAgeTagDao
 * - Added migration v7→v8
 */
@Module
@InstallIn(SingletonComponent::class)
object DatabaseModule {

    // ===== DATABASE =====

    @Provides
    @Singleton
    fun provideDatabase(
        @ApplicationContext context: Context
    ): AppDatabase =
        Room.databaseBuilder(
            context,
            AppDatabase::class.java,
            "sherpai.db"
        )
            // DEVELOPMENT MODE: Destructive migration (fresh install on schema change)
            .fallbackToDestructiveMigration(dropAllTables = true)

            // PRODUCTION MODE: Uncomment this and remove fallbackToDestructiveMigration()
            // .addMigrations(MIGRATION_7_8, MIGRATION_8_9, MIGRATION_9_10)

            .build()

    // ===== CORE DAOs =====

    @Provides
    fun provideImageDao(db: AppDatabase): ImageDao =
        db.imageDao()

    @Provides
    fun provideTagDao(db: AppDatabase): TagDao =
        db.tagDao()

    @Provides
    fun provideEventDao(db: AppDatabase): EventDao =
        db.eventDao()

    @Provides
    fun provideImageEventDao(db: AppDatabase): ImageEventDao =
        db.imageEventDao()

    @Provides
    fun provideImageAggregateDao(db: AppDatabase): ImageAggregateDao =
        db.imageAggregateDao()

    @Provides
    fun provideImageTagDao(db: AppDatabase): ImageTagDao =
        db.imageTagDao()

    // ===== FACE RECOGNITION DAOs =====

    @Provides
    fun providePersonDao(db: AppDatabase): PersonDao =
        db.personDao()

    @Provides
    fun provideFaceModelDao(db: AppDatabase): FaceModelDao =
        db.faceModelDao()

    @Provides
    fun providePhotoFaceTagDao(db: AppDatabase): PhotoFaceTagDao =
        db.photoFaceTagDao()

    @Provides
    fun providePersonAgeTagDao(db: AppDatabase): PersonAgeTagDao =
        db.personAgeTagDao()

    @Provides
    fun provideFaceCacheDao(db: AppDatabase): FaceCacheDao =
        db.faceCacheDao()

    @Provides
    fun provideUserFeedbackDao(db: AppDatabase): UserFeedbackDao =
        db.userFeedbackDao()

    // ===== COLLECTIONS DAOs =====

    @Provides
    fun provideCollectionDao(db: AppDatabase): CollectionDao =
        db.collectionDao()
}