package core

import (
	"context"

	"fr.latosa-escrima/core/models"
	"github.com/uptrace/bun"
)

type Permissions []models.Permission

func GetAllPermissions() Permissions {
	resources := []string{"users", "roles", "media", "events", "permissions", "shortcodes", "blogs", "locations"}
	var perms Permissions
	for _, resource := range resources {
		perms = append(perms, Permissions{
			{
				Resource: resource,
				Action:   "insert",
			},
			{
				Resource: resource,
				Action:   "update",
			},
			{
				Resource: resource,
				Action:   "delete",
			},
			{
				Resource: resource,
				Action:   "get",
			},
			{
				Resource: resource,
				Action:   "own:insert",
			},
			{
				Resource: resource,
				Action:   "own:update",
			},
			{
				Resource: resource,
				Action:   "own:delete",
			},
			{
				Resource: resource,
				Action:   "own:get",
			},
		}...)
	}
	return perms
}

func (perms Permissions) InsertAll(db *bun.DB, ctx context.Context) error {
	_, err := db.NewInsert().
		Model(&perms).
		Ignore().
		Exec(ctx)

	return err
}
