/*
 * Kiwix Android
 * Copyright (c) 2023 Kiwix <android.kiwix.org>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

package org.kiwix.kiwixmobile.core.data

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import org.kiwix.kiwixmobile.core.dao.HistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.HistoryRoomDaoCoverts
import org.kiwix.kiwixmobile.core.dao.NotesRoomDao
import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
import org.kiwix.kiwixmobile.core.dao.entities.HistoryRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.NotesRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity

@Suppress("UnnecessaryAbstractClass")
@Database(
  entities = [
    RecentSearchRoomEntity::class,
    HistoryRoomEntity::class,
    NotesRoomEntity::class
  ],
  version = 3,
  exportSchema = false
)
@TypeConverters(HistoryRoomDaoCoverts::class)
abstract class KiwixRoomDatabase : RoomDatabase() {
  abstract fun recentSearchRoomDao(): RecentSearchRoomDao
  abstract fun historyRoomDao(): HistoryRoomDao
  abstract fun notesRoomDao(): NotesRoomDao

  companion object {
    private var db: KiwixRoomDatabase? = null
    fun getInstance(context: Context): KiwixRoomDatabase {
      return db ?: synchronized(KiwixRoomDatabase::class) {
        return@getInstance db
          ?: Room.databaseBuilder(context, KiwixRoomDatabase::class.java, "KiwixRoom.db")
            // We have already database name called kiwix.db in order to avoid complexity we named
            // as kiwixRoom.db
            .addMigrations(MIGRATION_1_2, MIGRATION_2_3)
            .build()
      }
    }

    private val MIGRATION_1_2: Migration = object : Migration(1, 2) {
      override fun migrate(database: SupportSQLiteDatabase) {
        database.execSQL(
          """
            CREATE TABLE IF NOT EXISTS `HistoryRoomEntity`(
            `id` INTEGER NOT NULL,
            `timeStamp` INTEGER NOT NULL,
            `zimId` TEXT NOT NULL,
            `historyUrl` TEXT NOT NULL,
            `zimName` TEXT NOT NULL,
            `favicon` TEXT,
            `historyTitle` TEXT NOT NULL,
            `dateString` TEXT NOT NULL,
            `zimFilePath` TEXT NOT NULL,
            PRIMARY KEY (`id`)
          )
          """
        )
      }
    }

    @Suppress("MagicNumber")
    private val MIGRATION_2_3 = object : Migration(2, 3) {
      override fun migrate(database: SupportSQLiteDatabase) {
        database.execSQL(
          """
            CREATE TABLE IF NOT EXISTS `NotesRoomEntity`(
              `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
              `zimId` TEXT NOT NULL,
              `zimFilePath` TEXT,
              `zimUrl` TEXT NOT NULL,
              `noteTitle` TEXT NOT NULL,
              `noteFilePath` TEXT NOT NULL,
              `favicon` TEXT
            )
          """
        )

        database.execSQL(
          """
            CREATE UNIQUE INDEX IF NOT EXISTS `index_NotesRoomEntity_noteTitle` ON `NotesRoomEntity` (`noteTitle`)
            """
        )
      }
    }

    fun destroyInstance() {
      db = null
    }
  }
}
