package duckdb

import (
	"database/sql"
	"fmt"

	_ "github.com/marcboeker/go-duckdb"
)

// DuckDBOpen opens (or creates) a DuckDB database file.
// Pass an empty path or ":memory:" for an in-memory database.
// Returns a ready-to-use *sql.DB or an error.
func DuckDBOpen(path string) (*sql.DB, error) {
	dsn := path
	if dsn == "" {
		dsn = ":memory:"
	}
	db, err := sql.Open("duckdb", dsn)
	if err != nil {
		return nil, fmt.Errorf("duckdb_open: open %q: %w", dsn, err)
	}
	if err := db.Ping(); err != nil {
		db.Close()
		return nil, fmt.Errorf("duckdb_open: ping %q: %w", dsn, err)
	}
	return db, nil
}
