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

package postgres

import (
	"context"
)

const createOIDCSession = `-- name: CreateOIDCSession :one
INSERT INTO "oidc_sessions" (
    "sub",
    "access_token_hash",
    "refresh_token_hash",
    "scope",
    "client_id",
    "token_expires_at",
    "refresh_token_expires_at",
    "nonce",
    "userinfo_json"
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9
)
RETURNING sub, access_token_hash, refresh_token_hash, scope, client_id, token_expires_at, refresh_token_expires_at, nonce, userinfo_json
`

type CreateOIDCSessionParams struct {
	Sub                   string
	AccessTokenHash       string
	RefreshTokenHash      string
	Scope                 string
	ClientID              string
	TokenExpiresAt        int64
	RefreshTokenExpiresAt int64
	Nonce                 string
	UserinfoJson          string
}

func (q *Queries) CreateOIDCSession(ctx context.Context, arg CreateOIDCSessionParams) (OidcSession, error) {
	row := q.db.QueryRowContext(ctx, createOIDCSession,
		arg.Sub,
		arg.AccessTokenHash,
		arg.RefreshTokenHash,
		arg.Scope,
		arg.ClientID,
		arg.TokenExpiresAt,
		arg.RefreshTokenExpiresAt,
		arg.Nonce,
		arg.UserinfoJson,
	)
	var i OidcSession
	err := row.Scan(
		&i.Sub,
		&i.AccessTokenHash,
		&i.RefreshTokenHash,
		&i.Scope,
		&i.ClientID,
		&i.TokenExpiresAt,
		&i.RefreshTokenExpiresAt,
		&i.Nonce,
		&i.UserinfoJson,
	)
	return i, err
}

const deleteExpiredOIDCSessions = `-- name: DeleteExpiredOIDCSessions :exec
DELETE FROM "oidc_sessions"
WHERE "token_expires_at" < $1 AND "refresh_token_expires_at" < $2
`

type DeleteExpiredOIDCSessionsParams struct {
	TokenExpiresAt        int64
	RefreshTokenExpiresAt int64
}

func (q *Queries) DeleteExpiredOIDCSessions(ctx context.Context, arg DeleteExpiredOIDCSessionsParams) error {
	_, err := q.db.ExecContext(ctx, deleteExpiredOIDCSessions, arg.TokenExpiresAt, arg.RefreshTokenExpiresAt)
	return err
}

const deleteOIDCConsentByClientID = `-- name: DeleteOIDCConsentByClientID :exec
DELETE FROM "oidc_consents"
WHERE "client_id" = $1
`

func (q *Queries) DeleteOIDCConsentByClientID(ctx context.Context, clientID string) error {
	_, err := q.db.ExecContext(ctx, deleteOIDCConsentByClientID, clientID)
	return err
}

const deleteOIDCSessionBySub = `-- name: DeleteOIDCSessionBySub :exec
DELETE FROM "oidc_sessions"
WHERE "sub" = $1
`

func (q *Queries) DeleteOIDCSessionBySub(ctx context.Context, sub string) error {
	_, err := q.db.ExecContext(ctx, deleteOIDCSessionBySub, sub)
	return err
}

const getOIDCConsentByUsernameAndClientID = `-- name: GetOIDCConsentByUsernameAndClientID :one
SELECT username, client_id, scope, created_at FROM "oidc_consents"
WHERE "username" = $1 AND "client_id" = $2
`

type GetOIDCConsentByUsernameAndClientIDParams struct {
	Username string
	ClientID string
}

func (q *Queries) GetOIDCConsentByUsernameAndClientID(ctx context.Context, arg GetOIDCConsentByUsernameAndClientIDParams) (OidcConsent, error) {
	row := q.db.QueryRowContext(ctx, getOIDCConsentByUsernameAndClientID, arg.Username, arg.ClientID)
	var i OidcConsent
	err := row.Scan(
		&i.Username,
		&i.ClientID,
		&i.Scope,
		&i.CreatedAt,
	)
	return i, err
}

const getOIDCSessionByAccessTokenHash = `-- name: GetOIDCSessionByAccessTokenHash :one
SELECT sub, access_token_hash, refresh_token_hash, scope, client_id, token_expires_at, refresh_token_expires_at, nonce, userinfo_json FROM "oidc_sessions"
WHERE "access_token_hash" = $1
`

func (q *Queries) GetOIDCSessionByAccessTokenHash(ctx context.Context, accessTokenHash string) (OidcSession, error) {
	row := q.db.QueryRowContext(ctx, getOIDCSessionByAccessTokenHash, accessTokenHash)
	var i OidcSession
	err := row.Scan(
		&i.Sub,
		&i.AccessTokenHash,
		&i.RefreshTokenHash,
		&i.Scope,
		&i.ClientID,
		&i.TokenExpiresAt,
		&i.RefreshTokenExpiresAt,
		&i.Nonce,
		&i.UserinfoJson,
	)
	return i, err
}

const getOIDCSessionByRefreshTokenHash = `-- name: GetOIDCSessionByRefreshTokenHash :one
SELECT sub, access_token_hash, refresh_token_hash, scope, client_id, token_expires_at, refresh_token_expires_at, nonce, userinfo_json FROM "oidc_sessions"
WHERE "refresh_token_hash" = $1
`

func (q *Queries) GetOIDCSessionByRefreshTokenHash(ctx context.Context, refreshTokenHash string) (OidcSession, error) {
	row := q.db.QueryRowContext(ctx, getOIDCSessionByRefreshTokenHash, refreshTokenHash)
	var i OidcSession
	err := row.Scan(
		&i.Sub,
		&i.AccessTokenHash,
		&i.RefreshTokenHash,
		&i.Scope,
		&i.ClientID,
		&i.TokenExpiresAt,
		&i.RefreshTokenExpiresAt,
		&i.Nonce,
		&i.UserinfoJson,
	)
	return i, err
}

const getOIDCSessionBySub = `-- name: GetOIDCSessionBySub :one
SELECT sub, access_token_hash, refresh_token_hash, scope, client_id, token_expires_at, refresh_token_expires_at, nonce, userinfo_json FROM "oidc_sessions"
WHERE "sub" = $1
`

func (q *Queries) GetOIDCSessionBySub(ctx context.Context, sub string) (OidcSession, error) {
	row := q.db.QueryRowContext(ctx, getOIDCSessionBySub, sub)
	var i OidcSession
	err := row.Scan(
		&i.Sub,
		&i.AccessTokenHash,
		&i.RefreshTokenHash,
		&i.Scope,
		&i.ClientID,
		&i.TokenExpiresAt,
		&i.RefreshTokenExpiresAt,
		&i.Nonce,
		&i.UserinfoJson,
	)
	return i, err
}

const listOIDCConsents = `-- name: ListOIDCConsents :many
SELECT username, client_id, scope, created_at FROM "oidc_consents"
`

func (q *Queries) ListOIDCConsents(ctx context.Context) ([]OidcConsent, error) {
	rows, err := q.db.QueryContext(ctx, listOIDCConsents)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []OidcConsent
	for rows.Next() {
		var i OidcConsent
		if err := rows.Scan(
			&i.Username,
			&i.ClientID,
			&i.Scope,
			&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 updateOIDCSession = `-- name: UpdateOIDCSession :one
UPDATE "oidc_sessions" SET
    "access_token_hash" = $1,
    "refresh_token_hash" = $2,
    "scope" = $3,
    "client_id" = $4,
    "token_expires_at" = $5,
    "refresh_token_expires_at" = $6,
    "nonce" = $7,
    "userinfo_json" = $8
WHERE "sub" = $9
RETURNING sub, access_token_hash, refresh_token_hash, scope, client_id, token_expires_at, refresh_token_expires_at, nonce, userinfo_json
`

type UpdateOIDCSessionParams struct {
	AccessTokenHash       string
	RefreshTokenHash      string
	Scope                 string
	ClientID              string
	TokenExpiresAt        int64
	RefreshTokenExpiresAt int64
	Nonce                 string
	UserinfoJson          string
	Sub                   string
}

func (q *Queries) UpdateOIDCSession(ctx context.Context, arg UpdateOIDCSessionParams) (OidcSession, error) {
	row := q.db.QueryRowContext(ctx, updateOIDCSession,
		arg.AccessTokenHash,
		arg.RefreshTokenHash,
		arg.Scope,
		arg.ClientID,
		arg.TokenExpiresAt,
		arg.RefreshTokenExpiresAt,
		arg.Nonce,
		arg.UserinfoJson,
		arg.Sub,
	)
	var i OidcSession
	err := row.Scan(
		&i.Sub,
		&i.AccessTokenHash,
		&i.RefreshTokenHash,
		&i.Scope,
		&i.ClientID,
		&i.TokenExpiresAt,
		&i.RefreshTokenExpiresAt,
		&i.Nonce,
		&i.UserinfoJson,
	)
	return i, err
}

const upsertOIDCConsent = `-- name: UpsertOIDCConsent :one
INSERT INTO "oidc_consents" (
    "username",
    "client_id",
    "scope",
    "created_at"
) VALUES (
    $1, $2, $3, $4
)
ON CONFLICT ("username", "client_id")
DO UPDATE SET
    "scope" = excluded.scope,
    "created_at" = excluded.created_at
RETURNING username, client_id, scope, created_at
`

type UpsertOIDCConsentParams struct {
	Username  string
	ClientID  string
	Scope     string
	CreatedAt int64
}

func (q *Queries) UpsertOIDCConsent(ctx context.Context, arg UpsertOIDCConsentParams) (OidcConsent, error) {
	row := q.db.QueryRowContext(ctx, upsertOIDCConsent,
		arg.Username,
		arg.ClientID,
		arg.Scope,
		arg.CreatedAt,
	)
	var i OidcConsent
	err := row.Scan(
		&i.Username,
		&i.ClientID,
		&i.Scope,
		&i.CreatedAt,
	)
	return i, err
}
