package database

import (
	"fmt"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

func New(connectionString string) (*gorm.DB, error) {
	db, err := gorm.Open(postgres.Open(connectionString), &gorm.Config{})
	if err != nil {
		return nil, fmt.Errorf("error connecting to the database %w", err)
	}

	return db, nil
}

func Migrate(db *gorm.DB, tables ...any) error {
	err := db.AutoMigrate(tables...)
	if err != nil {
		return fmt.Errorf("error migrating the database %w", err)
	}
	return nil
}
