-- name: GetOIDCSessionBySub :one
SELECT * FROM "oidc_sessions"
WHERE "sub" = $1;

-- name: GetOIDCSessionByAccessTokenHash :one
SELECT * FROM "oidc_sessions"
WHERE "access_token_hash" = $1;

-- name: GetOIDCSessionByRefreshTokenHash :one
SELECT * FROM "oidc_sessions"
WHERE "refresh_token_hash" = $1;

-- 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 *;

-- name: DeleteOIDCSessionBySub :exec
DELETE FROM "oidc_sessions"
WHERE "sub" = $1;

-- name: DeleteExpiredOIDCSessions :exec
DELETE FROM "oidc_sessions"
WHERE "token_expires_at" < $1 AND "refresh_token_expires_at" < $2;

-- 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 *;

-- 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 *;

-- name: GetOIDCConsentByUsernameAndClientID :one
SELECT * FROM "oidc_consents"
WHERE "username" = $1 AND "client_id" = $2;

-- name: DeleteOIDCConsentByClientID :exec
DELETE FROM "oidc_consents"
WHERE "client_id" = $1;

-- name: ListOIDCConsents :many
SELECT * FROM "oidc_consents";
