package utils

import (
	"licserver/internal/model"

	"gorm.io/driver/sqlite"

	"gorm.io/gorm"
)

func InitDB(config *DatabaseConfig) (*gorm.DB, error) {

	db, err := gorm.Open(sqlite.Open(config.Path), &gorm.Config{})

	if err != nil {

		return nil, err

	}

	// 自动迁移数据库结构

	err = db.AutoMigrate(

		&model.User{},

		&model.Device{},

		&model.DeviceModel{},

		&model.PasswordResetToken{},

		&model.Captcha{},

		&model.FileUpload{},

		&model.UploadChunk{},

		&model.LicenseCode{},

		&model.LicenseLog{},

		&model.AccessToken{},

		&model.TokenLog{},
	)

	if err != nil {

		return nil, err

	}

	return db, nil

}
