package database

import (
	"whereismymoney/internal/config"
	"whereismymoney/internal/models"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

func Connect(cfg *config.Config) (*gorm.DB, error) {
	db, err := gorm.Open(sqlite.Open(cfg.Database.Path), &gorm.Config{})
	if err != nil {
		return nil, err
	}

	// Auto-migrate the schema
	if err := models.Migrate(db); err != nil {
		return nil, err
	}

	return db, nil
}
