package permissions

import "database/sql"

func SeedAdminRoleDepartments(db *sql.DB) error {

	var adminID int
	var err error

	// Admin role id al
	err = db.QueryRow(`
		SELECT id
		FROM dfrole
		WHERE code = 'admin'
	`).Scan(&adminID)

	if err != nil {
		return err
	}

	// Seed
	_, err = db.Exec(`
INSERT INTO mk_sys_role_department_permissions
  (role_id, department_code, module_code, action, allowed)

SELECT
  $1,
  d.code,
  r.module_code,
  r.action,
  true
FROM mk_dprt d
CROSS JOIN mk_sys_routes r

ON CONFLICT (role_id, department_code, module_code, action)
DO NOTHING
`, adminID)

	return err
}
