package utils

import (
	"errors"
	"strings"

	"github.com/jackc/pgx/v5/pgconn"
)

// IsDuplicateKeyError checks if the error is a PostgreSQL duplicate key violation
func IsDuplicateKeyError(err error) bool {
	var pgErr *pgconn.PgError
	if errors.As(err, &pgErr) {
		// 23505 is the PostgreSQL error code for unique_violation
		return pgErr.Code == "23505"
	}
	// Fallback for other drivers or if error wrapping is different
	return strings.Contains(err.Error(), "duplicate key value violates unique constraint")
}
