package xyz.r0r5chach.dermy_app.db

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

class DermyDatabase(context: Context?) :
    SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {

    override fun onCreate(sqLiteDatabase: SQLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE ${Tables.MOLES} (" +
            "id TEXT PRIMARY KEY," +
            "userId TEXT," +
            "dateCreated TEXT," +
            "location TEXT," +
            "benign TEXT," +
            "malignant TEXT)")

        sqLiteDatabase.execSQL("CREATE TABLE ${Tables.LOGS} (" +
            "id TEXT PRIMARY KEY," +
            "moleId TEXT," +
            "dateCreated TEXT," +
            "contents TEXT)")
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        for(table in Tables.values()) {
            db!!.execSQL("DROP TABLE IF EXISTS $table")
        }
        onCreate(db!!)
    }

    companion object {
        private const val DB_VERSION = 1
        private const val DB_NAME = "dermy"
    }
}
