// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: audit.sql

package auditsqlc

import (
	"context"
	"net/netip"

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

const countAuditLog = `-- name: CountAuditLog :one
SELECT COUNT(*)
FROM audit_logs
WHERE ($1::uuid IS NULL OR user_id = $1)
  AND ($2::text IS NULL OR action = $2)
  AND ($3::text IS NULL OR action LIKE $3 || '%')
  AND ($4::text IS NULL OR target_type = $4)
  AND ($5::text IS NULL OR target_id = $5)
  AND ($6::timestamptz IS NULL OR created_at >= $6)
  AND ($7::timestamptz IS NULL OR created_at < $7)
`

type CountAuditLogParams struct {
	UserID       pgtype.UUID        `json:"user_id"`
	Action       *string            `json:"action"`
	ActionPrefix *string            `json:"action_prefix"`
	TargetType   *string            `json:"target_type"`
	TargetID     *string            `json:"target_id"`
	FromTs       pgtype.Timestamptz `json:"from_ts"`
	ToTs         pgtype.Timestamptz `json:"to_ts"`
}

func (q *Queries) CountAuditLog(ctx context.Context, arg CountAuditLogParams) (int64, error) {
	row := q.db.QueryRow(ctx, countAuditLog,
		arg.UserID,
		arg.Action,
		arg.ActionPrefix,
		arg.TargetType,
		arg.TargetID,
		arg.FromTs,
		arg.ToTs,
	)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const deleteAuditLogsBefore = `-- name: DeleteAuditLogsBefore :execrows
DELETE FROM audit_logs WHERE created_at < $1
`

func (q *Queries) DeleteAuditLogsBefore(ctx context.Context, createdAt pgtype.Timestamptz) (int64, error) {
	result, err := q.db.Exec(ctx, deleteAuditLogsBefore, createdAt)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const insertAuditLog = `-- name: InsertAuditLog :exec
INSERT INTO audit_logs (user_id, action, target_type, target_id, ip, metadata)
VALUES ($1, $2, $3, $4, $5, $6)
`

type InsertAuditLogParams struct {
	UserID     pgtype.UUID `json:"user_id"`
	Action     string      `json:"action"`
	TargetType *string     `json:"target_type"`
	TargetID   *string     `json:"target_id"`
	Ip         *netip.Addr `json:"ip"`
	Metadata   []byte      `json:"metadata"`
}

func (q *Queries) InsertAuditLog(ctx context.Context, arg InsertAuditLogParams) error {
	_, err := q.db.Exec(ctx, insertAuditLog,
		arg.UserID,
		arg.Action,
		arg.TargetType,
		arg.TargetID,
		arg.Ip,
		arg.Metadata,
	)
	return err
}

const listAuditLog = `-- name: ListAuditLog :many
SELECT id, user_id, action, target_type, target_id, ip, metadata, created_at
FROM audit_logs
WHERE ($3::uuid IS NULL OR user_id = $3)
  AND ($4::text IS NULL OR action = $4)
  AND ($5::text IS NULL OR action LIKE $5 || '%')
  AND ($6::text IS NULL OR target_type = $6)
  AND ($7::text IS NULL OR target_id = $7)
  AND ($8::timestamptz IS NULL OR created_at >= $8)
  AND ($9::timestamptz IS NULL OR created_at < $9)
ORDER BY created_at DESC, id DESC
LIMIT $1 OFFSET $2
`

type ListAuditLogParams struct {
	Limit        int32              `json:"limit"`
	Offset       int32              `json:"offset"`
	UserID       pgtype.UUID        `json:"user_id"`
	Action       *string            `json:"action"`
	ActionPrefix *string            `json:"action_prefix"`
	TargetType   *string            `json:"target_type"`
	TargetID     *string            `json:"target_id"`
	FromTs       pgtype.Timestamptz `json:"from_ts"`
	ToTs         pgtype.Timestamptz `json:"to_ts"`
}

// Paginated read with optional filters. NULL filter args match everything
// (sqlc.narg renders as a nullable parameter; COALESCE/IS NULL short-circuits).
func (q *Queries) ListAuditLog(ctx context.Context, arg ListAuditLogParams) ([]AuditLog, error) {
	rows, err := q.db.Query(ctx, listAuditLog,
		arg.Limit,
		arg.Offset,
		arg.UserID,
		arg.Action,
		arg.ActionPrefix,
		arg.TargetType,
		arg.TargetID,
		arg.FromTs,
		arg.ToTs,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []AuditLog
	for rows.Next() {
		var i AuditLog
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Action,
			&i.TargetType,
			&i.TargetID,
			&i.Ip,
			&i.Metadata,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
