// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.28.0
// source: rolepermissions.sql

package db

import (
	"context"
)

const createRolePermission = `-- name: CreateRolePermission :one
INSERT INTO role_permissions (
    role_id,
    permissions
 ) VALUES (
    $1, $2
 ) 
 RETURNING id, role_id, permissions, created_at
`

type CreateRolePermissionParams struct {
	RoleID      int32  `json:"role_id"`
	Permissions string `json:"permissions"`
}

func (q *Queries) CreateRolePermission(ctx context.Context, arg CreateRolePermissionParams) (RolePermission, error) {
	row := q.db.QueryRowContext(ctx, createRolePermission, arg.RoleID, arg.Permissions)
	var i RolePermission
	err := row.Scan(
		&i.ID,
		&i.RoleID,
		&i.Permissions,
		&i.CreatedAt,
	)
	return i, err
}

const deleteRolePermission = `-- name: DeleteRolePermission :exec
DELETE FROM role_permissions WHERE role_id = $1
`

func (q *Queries) DeleteRolePermission(ctx context.Context, roleID int32) error {
	_, err := q.db.ExecContext(ctx, deleteRolePermission, roleID)
	return err
}

const getPermission = `-- name: GetPermission :many
SELECT permissions FROM role_permissions
WHERE role_id = $1
`

func (q *Queries) GetPermission(ctx context.Context, roleID int32) ([]string, error) {
	rows, err := q.db.QueryContext(ctx, getPermission, roleID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []string{}
	for rows.Next() {
		var permissions string
		if err := rows.Scan(&permissions); err != nil {
			return nil, err
		}
		items = append(items, permissions)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const isAllowed = `-- name: IsAllowed :one
SELECT EXISTS (
    SELECT 1
    FROM user_roles ur 
    JOIN role_permissions rp ON ur.role_id = rp.role_id 
    WHERE ur.user_id = $1 AND rp.permissions = $2
)
`

type IsAllowedParams struct {
	UserID      int32  `json:"user_id"`
	Permissions string `json:"permissions"`
}

func (q *Queries) IsAllowed(ctx context.Context, arg IsAllowedParams) (bool, error) {
	row := q.db.QueryRowContext(ctx, isAllowed, arg.UserID, arg.Permissions)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const listRolePermission = `-- name: ListRolePermission :many
SELECT id, role_id, permissions, created_at FROM role_permissions
ORDER BY role_id
`

func (q *Queries) ListRolePermission(ctx context.Context) ([]RolePermission, error) {
	rows, err := q.db.QueryContext(ctx, listRolePermission)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []RolePermission{}
	for rows.Next() {
		var i RolePermission
		if err := rows.Scan(
			&i.ID,
			&i.RoleID,
			&i.Permissions,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateRolePermission = `-- name: UpdateRolePermission :exec
UPDATE role_permissions
  set 
  permissions = $2
WHERE role_id = $1
`

type UpdateRolePermissionParams struct {
	RoleID      int32  `json:"role_id"`
	Permissions string `json:"permissions"`
}

func (q *Queries) UpdateRolePermission(ctx context.Context, arg UpdateRolePermissionParams) error {
	_, err := q.db.ExecContext(ctx, updateRolePermission, arg.RoleID, arg.Permissions)
	return err
}
