// Copyright 2023 Harran Ali <harran.m@gmail.com>. All rights reserved.
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
// Use of this source code is governed by MIT-style
// license that can be found in the LICENSE file.

package main

import (
	"errors"

	"git.smarteching.com/goffee/core"
	"git.smarteching.com/jacs/goffeetabler/models"
	"git.smarteching.com/jacs/goffeetabler/utils"
	"gorm.io/gorm"
)

func RunAutoMigrations() {
	db := core.ResolveGorm()
	//##############################
	//# Models auto migration  #####
	//##############################

	// Add auto migrations for your models here...
	db.AutoMigrate(&models.UserRole{})
	db.AutoMigrate(&models.Role{})
	db.AutoMigrate(&models.RolePermission{})
	db.AutoMigrate(&models.Permission{})

	// End your auto migrations

	// Create seed data data, DO NOT TOUCH
	if err := db.AutoMigrate(&models.User{}); err == nil && db.Migrator().HasTable(&models.User{}) {
		if err := db.First(&models.User{}).Error; errors.Is(err, gorm.ErrRecordNotFound) {
			utils.CreateSeedData()
		}
	}

}
