package cmd

import (
	"database/sql"
	"fmt"

	_ "modernc.org/sqlite"
)

func updateVscdbPureGo(dbPath string, ids map[string]string) error {
	db, err := sql.Open("sqlite", dbPath)
	if err != nil {
		return fmt.Errorf("open db: %w", err)
	}
	defer db.Close()

	_, err = db.Exec(`CREATE TABLE IF NOT EXISTS ItemTable (key TEXT PRIMARY KEY, value TEXT NOT NULL)`)
	if err != nil {
		return fmt.Errorf("create table: %w", err)
	}

	for k, v := range ids {
		_, err = db.Exec(`INSERT OR REPLACE INTO ItemTable (key, value) VALUES (?, ?)`, k, v)
		if err != nil {
			return fmt.Errorf("insert %s: %w", k, err)
		}
	}
	return nil
}
