// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.27.0
// source: system.sql

package db

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const clearActivityLogs = `-- name: ClearActivityLogs :exec
DELETE FROM activity_logs
`

func (q *Queries) ClearActivityLogs(ctx context.Context) error {
	_, err := q.db.Exec(ctx, clearActivityLogs)
	return err
}

const countActivityLogs = `-- name: CountActivityLogs :one
SELECT count(*) FROM activity_logs
WHERE ($1 = '' OR module = $1)
  AND ($2 = 0 OR user_id = $2)
  AND ($3 = '' OR created_at >= NULLIF($3, '')::timestamptz)
  AND ($4 = '' OR created_at <= NULLIF($4, '')::timestamptz)
`

type CountActivityLogsParams struct {
	Column1 interface{} `json:"column_1"`
	Column2 interface{} `json:"column_2"`
	Column3 interface{} `json:"column_3"`
	Column4 interface{} `json:"column_4"`
}

func (q *Queries) CountActivityLogs(ctx context.Context, arg CountActivityLogsParams) (int64, error) {
	row := q.db.QueryRow(ctx, countActivityLogs,
		arg.Column1,
		arg.Column2,
		arg.Column3,
		arg.Column4,
	)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const countUsersByRole = `-- name: CountUsersByRole :one
SELECT count(*) FROM users WHERE role = $1
`

func (q *Queries) CountUsersByRole(ctx context.Context, role string) (int64, error) {
	row := q.db.QueryRow(ctx, countUsersByRole, role)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const createActivityLog = `-- name: CreateActivityLog :exec
INSERT INTO activity_logs
(user_id, module, action, description, model_type, model_id,
 old_values, new_values, ip_address, user_agent, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, now(), now())
`

type CreateActivityLogParams struct {
	UserID      pgtype.Int8 `json:"user_id"`
	Module      pgtype.Text `json:"module"`
	Action      pgtype.Text `json:"action"`
	Description pgtype.Text `json:"description"`
	ModelType   pgtype.Text `json:"model_type"`
	ModelID     pgtype.Int8 `json:"model_id"`
	OldValues   []byte      `json:"old_values"`
	NewValues   []byte      `json:"new_values"`
	IpAddress   pgtype.Text `json:"ip_address"`
	UserAgent   pgtype.Text `json:"user_agent"`
}

func (q *Queries) CreateActivityLog(ctx context.Context, arg CreateActivityLogParams) error {
	_, err := q.db.Exec(ctx, createActivityLog,
		arg.UserID,
		arg.Module,
		arg.Action,
		arg.Description,
		arg.ModelType,
		arg.ModelID,
		arg.OldValues,
		arg.NewValues,
		arg.IpAddress,
		arg.UserAgent,
	)
	return err
}

const createRole = `-- name: CreateRole :one
INSERT INTO roles (name, created_at, updated_at) VALUES ($1, now(), now())
RETURNING id, name, created_at, updated_at
`

func (q *Queries) CreateRole(ctx context.Context, name string) (Role, error) {
	row := q.db.QueryRow(ctx, createRole, name)
	var i Role
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const deleteRole = `-- name: DeleteRole :execrows
DELETE FROM roles WHERE id = $1
`

func (q *Queries) DeleteRole(ctx context.Context, id int64) (int64, error) {
	result, err := q.db.Exec(ctx, deleteRole, id)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const deleteRolePermissionsByRole = `-- name: DeleteRolePermissionsByRole :exec
DELETE FROM role_permissions WHERE role = $1
`

func (q *Queries) DeleteRolePermissionsByRole(ctx context.Context, role string) error {
	_, err := q.db.Exec(ctx, deleteRolePermissionsByRole, role)
	return err
}

const getRoleByID = `-- name: GetRoleByID :one
SELECT id, name, created_at, updated_at FROM roles WHERE id = $1
`

func (q *Queries) GetRoleByID(ctx context.Context, id int64) (Role, error) {
	row := q.db.QueryRow(ctx, getRoleByID, id)
	var i Role
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getRoleByName = `-- name: GetRoleByName :one
SELECT id, name, created_at, updated_at FROM roles WHERE name = $1
`

func (q *Queries) GetRoleByName(ctx context.Context, name string) (Role, error) {
	row := q.db.QueryRow(ctx, getRoleByName, name)
	var i Role
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listActivityLogs = `-- name: ListActivityLogs :many
SELECT l.id, l.user_id, l.module, l.action, l.description, l.model_type, l.model_id,
       l.old_values, l.new_values, l.ip_address, l.user_agent, l.created_at, l.updated_at,
       COALESCE(u.name, '') AS user_name
FROM activity_logs l
LEFT JOIN users u ON u.id = l.user_id
WHERE ($1 = '' OR l.module = $1)
  AND ($2 = 0 OR l.user_id = $2)
  AND ($3 = '' OR l.created_at >= NULLIF($3, '')::timestamptz)
  AND ($4 = '' OR l.created_at <= NULLIF($4, '')::timestamptz)
ORDER BY l.created_at DESC, l.id DESC
LIMIT $5 OFFSET $6
`

type ListActivityLogsParams struct {
	Column1 interface{} `json:"column_1"`
	Column2 interface{} `json:"column_2"`
	Column3 interface{} `json:"column_3"`
	Column4 interface{} `json:"column_4"`
	Limit   int32       `json:"limit"`
	Offset  int32       `json:"offset"`
}

type ListActivityLogsRow struct {
	ID          int64              `json:"id"`
	UserID      pgtype.Int8        `json:"user_id"`
	Module      pgtype.Text        `json:"module"`
	Action      pgtype.Text        `json:"action"`
	Description pgtype.Text        `json:"description"`
	ModelType   pgtype.Text        `json:"model_type"`
	ModelID     pgtype.Int8        `json:"model_id"`
	OldValues   []byte             `json:"old_values"`
	NewValues   []byte             `json:"new_values"`
	IpAddress   pgtype.Text        `json:"ip_address"`
	UserAgent   pgtype.Text        `json:"user_agent"`
	CreatedAt   pgtype.Timestamptz `json:"created_at"`
	UpdatedAt   pgtype.Timestamptz `json:"updated_at"`
	UserName    string             `json:"user_name"`
}

func (q *Queries) ListActivityLogs(ctx context.Context, arg ListActivityLogsParams) ([]ListActivityLogsRow, error) {
	rows, err := q.db.Query(ctx, listActivityLogs,
		arg.Column1,
		arg.Column2,
		arg.Column3,
		arg.Column4,
		arg.Limit,
		arg.Offset,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListActivityLogsRow
	for rows.Next() {
		var i ListActivityLogsRow
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Module,
			&i.Action,
			&i.Description,
			&i.ModelType,
			&i.ModelID,
			&i.OldValues,
			&i.NewValues,
			&i.IpAddress,
			&i.UserAgent,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.UserName,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listRoles = `-- name: ListRoles :many
SELECT id, name, created_at, updated_at FROM roles ORDER BY id
`

func (q *Queries) ListRoles(ctx context.Context) ([]Role, error) {
	rows, err := q.db.Query(ctx, listRoles)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Role
	for rows.Next() {
		var i Role
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateRole = `-- name: UpdateRole :execrows
UPDATE roles SET name = $2, updated_at = now() WHERE id = $1
`

type UpdateRoleParams struct {
	ID   int64  `json:"id"`
	Name string `json:"name"`
}

func (q *Queries) UpdateRole(ctx context.Context, arg UpdateRoleParams) (int64, error) {
	result, err := q.db.Exec(ctx, updateRole, arg.ID, arg.Name)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const updateRolePermissionsRole = `-- name: UpdateRolePermissionsRole :execrows
UPDATE role_permissions SET role = $2 WHERE role = $1
`

type UpdateRolePermissionsRoleParams struct {
	Role   string `json:"role"`
	Role_2 string `json:"role_2"`
}

func (q *Queries) UpdateRolePermissionsRole(ctx context.Context, arg UpdateRolePermissionsRoleParams) (int64, error) {
	result, err := q.db.Exec(ctx, updateRolePermissionsRole, arg.Role, arg.Role_2)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const updateUsersRole = `-- name: UpdateUsersRole :execrows
UPDATE users SET role = $2, updated_at = now() WHERE role = $1
`

type UpdateUsersRoleParams struct {
	Role   string `json:"role"`
	Role_2 string `json:"role_2"`
}

func (q *Queries) UpdateUsersRole(ctx context.Context, arg UpdateUsersRoleParams) (int64, error) {
	result, err := q.db.Exec(ctx, updateUsersRole, arg.Role, arg.Role_2)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}
