package com.nurulwafa.nufakuproject;

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

public class RegisterDBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "UserDatabase.db";
    private static final int DATABASE_VERSION = 1;

    public RegisterDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TIDAK perlu membuat tabel di sini, karena kamu pakai tb_santri dari awal (imported / prepopulated)
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Kosongkan juga kalau kamu nggak perlu upgrade schema
    }

    // ✅ Mengecek login dari tb_santri berdasarkan email/nis dan password
    public boolean checkUser(String emailOrNis, String password) {
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "SELECT * FROM tb_santri WHERE (email = ? OR nis = ?) AND password = ?";
        Cursor cursor = db.rawQuery(query, new String[]{emailOrNis, emailOrNis, password});

        boolean result = cursor.moveToFirst();
        cursor.close();
        db.close();

        return result;
    }

    // ✅ Ambil id_santri setelah login berhasil
    public String getUserId(String emailOrNis) {
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "SELECT id_santri FROM tb_santri WHERE email = ? OR nis = ?";
        Cursor cursor = db.rawQuery(query, new String[]{emailOrNis, emailOrNis});

        if (cursor.moveToFirst()) {
            String id = cursor.getString(0);
            cursor.close();
            return id;
        }

        cursor.close();
        return null;
    }
}
