package role

import (
	"bsm/full/module/base/mgt/internal/libs"
	"bsm/full/module/base/mgt/internal/models"
	"bsm/full/module/base/mgt/internal/types"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/infra"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func PermissionsFetch(c *gin.Context) {
	var (
		request = types.FetchBase{}
		data    = make([]models.MgtRole, 0)
		db      = models.DBService.Model(&models.MgtRole{})
	)
	if err := c.ShouldBindJSON(&request); err != nil {
		infra.Response.Error(c, errcode.ErrJsonUnmarshal)
		return
	}

	if err := libs.ValidateStruct(&request); err != nil {
		infra.Response.Error(c, errcode.ErrInvalidArgument)
		return
	}
	if request.ID != 0 {
		db = db.Where("id = ?", request.ID)
	}

	if err := db.Select("id", "identity", "name").Preload("Permissions", func(db *gorm.DB) *gorm.DB {
		return db.Select("id", "identity", "title", "title_en", "sort_key", "parent_id")
	}).Find(&data).Error; err != nil {
		infra.Response.Error(c, errcode.ErrDB)
		return
	}
	infra.Response.Success(c, data)
}

func PermissionsFetchTree(c *gin.Context) {
	var (
		request = types.FetchBase{}
		data    = make([]models.MgtRole, 0)
		db      = models.DBService.Model(&models.MgtRole{})
	)

	if err := c.ShouldBindJSON(&request); err != nil {
		infra.Response.Error(c, errcode.ErrJsonUnmarshal)
		return
	}

	if err := libs.ValidateStruct(&request); err != nil {
		infra.Response.Error(c, errcode.ErrInvalidArgument)
		return
	}

	if request.ID != 0 {
		db = db.Where("id = ?", request.ID)
	}

	if err := db.Select("id", "identity", "name").Preload("Application", func(db *gorm.DB) *gorm.DB {
		db = db.Select("id", "identity", "title")
		if request.Workspace != "" {
			db = db.Where("workspace = ?", request.Workspace)
		}
		return db.Preload("RolesPmn", func(db *gorm.DB) *gorm.DB {
			return db.Select("id", "identity", "title", "title_en", "sort_key", "parent_id", "menu_path", "code", "component", "is_web_page", "is_new_tab", "web_url").
				Where("parent_id=0").Order("sort_key").
				Preload("Children", func(db *gorm.DB) *gorm.DB {
					return db.Select("id", "identity", "title", "title_en", "sort_key", "parent_id", "menu_path", "code", "component", "is_web_page", "is_new_tab", "web_url").Order("sort_key")
				})
		})
	}).Find(&data).Error; err != nil {
		infra.Response.Error(c, errcode.ErrDB)
		return
	}

	infra.Response.Success(c, data)
}

func ApplicationsFetch(c *gin.Context) {
	var (
		request = types.FetchBase{}
		data    = make([]models.MgtRole, 0)
		db      = models.DBService.Model(&models.MgtRole{})
	)
	if err := c.ShouldBindJSON(&request); err != nil {
		infra.Response.Error(c, errcode.ErrJsonUnmarshal)
		return
	}

	if err := libs.ValidateStruct(&request); err != nil {
		infra.Response.Error(c, errcode.ErrInvalidArgument)
		return
	}
	if request.ID != 0 {
		db = db.Where("id = ?", request.ID)
	}

	if err := db.Select("id", "identity", "name").Preload("Application").Find(&data).Error; err != nil {
		infra.Response.Error(c, errcode.ErrDB)
		return
	}
	infra.Response.Success(c, data)
}

func UserFetch(c *gin.Context) {
	var (
		request = types.FetchBase{}
		data    = make([]models.MgtRole, 0)
		db      = models.DBService.Model(&models.MgtRole{})
	)
	if err := c.ShouldBindJSON(&request); err != nil {
		infra.Response.Error(c, errcode.ErrJsonUnmarshal)
		return
	}

	if err := libs.ValidateStruct(&request); err != nil {
		infra.Response.Error(c, errcode.ErrInvalidArgument)
		return
	}
	if request.ID != 0 {
		db = db.Where("id = ?", request.ID)
	}
	if err := db.Select("id", "identity", "name").Preload("Users", func(db *gorm.DB) *gorm.DB {
		return db.Select("id", "identity", "name")
	}).Find(&data).Error; err != nil {
		infra.Response.Error(c, errcode.ErrDB)
		return
	}
	infra.Response.Success(c, data)
}
