package com.secureline.notes.data;

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

public class NotesDatabaseHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "secureline_notes.db";
    private static final int VERSION = 1;

    public NotesDatabaseHelper(Context ctx) {
        super(ctx, DB_NAME, null, VERSION);
    }

    @Override
    public void onConfigure(SQLiteDatabase db) {
        super.onConfigure(db);

        // 🔥 WAL = bedre performance + mindre corruption risk
        db.enableWriteAheadLogging();

        // 🔒 future-proof (hvis du tilføjer relations senere)
        db.setForeignKeyConstraintsEnabled(true);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(
                "CREATE TABLE notes (" +
                        "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                        "title TEXT NOT NULL DEFAULT ''," +
                        "content BLOB NOT NULL," +
                        "updated INTEGER NOT NULL" +
                        ")"
        );

        // 🔥 performance (du bruger ORDER BY updated)
        db.execSQL(
                "CREATE INDEX idx_notes_updated ON notes(updated DESC)"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 🚫 no-op (VERSION = 1)
    }
}
