// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.30.0
// source: activity_logs.sql

package dbgen

import (
	"context"

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

const CreateActivityLog = `-- name: CreateActivityLog :one
INSERT INTO activity_logs (
    actor_id, actor_role, action, resource_type, resource_id,
    message, metadata, ip_address, user_agent
) VALUES (
    $1, $2,
    $3, $4, $5,
    $6, COALESCE($7, '{}'::jsonb),
    $8, $9
)
RETURNING id, actor_id, actor_role, action, resource_type, resource_id, message, metadata, ip_address, user_agent, created_at
`

type CreateActivityLogParams struct {
	ActorID      pgtype.Int8 `json:"actor_id"`
	ActorRole    pgtype.Text `json:"actor_role"`
	Action       string      `json:"action"`
	ResourceType string      `json:"resource_type"`
	ResourceID   pgtype.Int8 `json:"resource_id"`
	Message      pgtype.Text `json:"message"`
	Metadata     interface{} `json:"metadata"`
	IpAddress    pgtype.Text `json:"ip_address"`
	UserAgent    pgtype.Text `json:"user_agent"`
}

func (q *Queries) CreateActivityLog(ctx context.Context, arg CreateActivityLogParams) (ActivityLog, error) {
	row := q.db.QueryRow(ctx, CreateActivityLog,
		arg.ActorID,
		arg.ActorRole,
		arg.Action,
		arg.ResourceType,
		arg.ResourceID,
		arg.Message,
		arg.Metadata,
		arg.IpAddress,
		arg.UserAgent,
	)
	var i ActivityLog
	err := row.Scan(
		&i.ID,
		&i.ActorID,
		&i.ActorRole,
		&i.Action,
		&i.ResourceType,
		&i.ResourceID,
		&i.Message,
		&i.Metadata,
		&i.IpAddress,
		&i.UserAgent,
		&i.CreatedAt,
	)
	return i, err
}

const GetActivityLogByID = `-- name: GetActivityLogByID :one
SELECT id, actor_id, actor_role, action, resource_type, resource_id, message, metadata, ip_address, user_agent, created_at FROM activity_logs WHERE id = $1
`

func (q *Queries) GetActivityLogByID(ctx context.Context, id int64) (ActivityLog, error) {
	row := q.db.QueryRow(ctx, GetActivityLogByID, id)
	var i ActivityLog
	err := row.Scan(
		&i.ID,
		&i.ActorID,
		&i.ActorRole,
		&i.Action,
		&i.ResourceType,
		&i.ResourceID,
		&i.Message,
		&i.Metadata,
		&i.IpAddress,
		&i.UserAgent,
		&i.CreatedAt,
	)
	return i, err
}

const ListActivityLogs = `-- name: ListActivityLogs :many
SELECT
    COUNT(*) OVER() AS total_count,
    al.id,
    al.actor_id,
    al.actor_role,
    al.action,
    al.resource_type,
    al.resource_id,
    al.message,
    al.metadata,
    al.ip_address,
    al.user_agent,
    al.created_at
FROM activity_logs al
WHERE
    ($1::bigint IS NULL OR al.actor_id = $1)
    AND ($2::text IS NULL OR al.action = $2)
    AND ($3::text IS NULL OR al.resource_type = $3)
    AND ($4::bigint IS NULL OR al.resource_id = $4)
    AND ($5::timestamptz IS NULL OR al.created_at >= $5)
    AND ($6::timestamptz IS NULL OR al.created_at <= $6)
ORDER BY al.created_at DESC
LIMIT $8 OFFSET $7
`

type ListActivityLogsParams struct {
	FilterActorID      pgtype.Int8        `json:"filter_actor_id"`
	FilterAction       pgtype.Text        `json:"filter_action"`
	FilterResourceType pgtype.Text        `json:"filter_resource_type"`
	FilterResourceID   pgtype.Int8        `json:"filter_resource_id"`
	FilterAfter        pgtype.Timestamptz `json:"filter_after"`
	FilterBefore       pgtype.Timestamptz `json:"filter_before"`
	LogOffset          int32              `json:"log_offset"`
	LogLimit           int32              `json:"log_limit"`
}

type ListActivityLogsRow struct {
	TotalCount   int64              `json:"total_count"`
	ID           int64              `json:"id"`
	ActorID      pgtype.Int8        `json:"actor_id"`
	ActorRole    pgtype.Text        `json:"actor_role"`
	Action       string             `json:"action"`
	ResourceType string             `json:"resource_type"`
	ResourceID   pgtype.Int8        `json:"resource_id"`
	Message      pgtype.Text        `json:"message"`
	Metadata     []byte             `json:"metadata"`
	IpAddress    pgtype.Text        `json:"ip_address"`
	UserAgent    pgtype.Text        `json:"user_agent"`
	CreatedAt    pgtype.Timestamptz `json:"created_at"`
}

func (q *Queries) ListActivityLogs(ctx context.Context, arg ListActivityLogsParams) ([]ListActivityLogsRow, error) {
	rows, err := q.db.Query(ctx, ListActivityLogs,
		arg.FilterActorID,
		arg.FilterAction,
		arg.FilterResourceType,
		arg.FilterResourceID,
		arg.FilterAfter,
		arg.FilterBefore,
		arg.LogOffset,
		arg.LogLimit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListActivityLogsRow
	for rows.Next() {
		var i ListActivityLogsRow
		if err := rows.Scan(
			&i.TotalCount,
			&i.ID,
			&i.ActorID,
			&i.ActorRole,
			&i.Action,
			&i.ResourceType,
			&i.ResourceID,
			&i.Message,
			&i.Metadata,
			&i.IpAddress,
			&i.UserAgent,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
