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

package sqlc

import (
	"context"

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

const clearMCPOAuthTokens = `-- name: ClearMCPOAuthTokens :exec
UPDATE mcp_oauth_tokens
SET access_token = '',
    refresh_token = '',
    expires_at = NULL,
    scope = '',
    pkce_code_verifier = '',
    state_param = '',
    redirect_uri = '',
    updated_at = now()
WHERE connection_id = $1
`

func (q *Queries) ClearMCPOAuthTokens(ctx context.Context, connectionID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, clearMCPOAuthTokens, connectionID)
	return err
}

const deleteMCPOAuthToken = `-- name: DeleteMCPOAuthToken :exec
DELETE FROM mcp_oauth_tokens
WHERE connection_id = $1
`

func (q *Queries) DeleteMCPOAuthToken(ctx context.Context, connectionID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteMCPOAuthToken, connectionID)
	return err
}

const getMCPOAuthToken = `-- name: GetMCPOAuthToken :one
SELECT id, connection_id, resource_metadata_url, authorization_server_url,
       authorization_endpoint, token_endpoint, registration_endpoint,
       scopes_supported, client_id, client_secret, access_token, refresh_token,
       token_type, expires_at, scope, pkce_code_verifier, state_param,
       resource_uri, redirect_uri, created_at, updated_at
FROM mcp_oauth_tokens
WHERE connection_id = $1
LIMIT 1
`

func (q *Queries) GetMCPOAuthToken(ctx context.Context, connectionID pgtype.UUID) (McpOauthToken, error) {
	row := q.db.QueryRow(ctx, getMCPOAuthToken, connectionID)
	var i McpOauthToken
	err := row.Scan(
		&i.ID,
		&i.ConnectionID,
		&i.ResourceMetadataUrl,
		&i.AuthorizationServerUrl,
		&i.AuthorizationEndpoint,
		&i.TokenEndpoint,
		&i.RegistrationEndpoint,
		&i.ScopesSupported,
		&i.ClientID,
		&i.ClientSecret,
		&i.AccessToken,
		&i.RefreshToken,
		&i.TokenType,
		&i.ExpiresAt,
		&i.Scope,
		&i.PkceCodeVerifier,
		&i.StateParam,
		&i.ResourceUri,
		&i.RedirectUri,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getMCPOAuthTokenByState = `-- name: GetMCPOAuthTokenByState :one
SELECT id, connection_id, resource_metadata_url, authorization_server_url,
       authorization_endpoint, token_endpoint, registration_endpoint,
       scopes_supported, client_id, client_secret, access_token, refresh_token,
       token_type, expires_at, scope, pkce_code_verifier, state_param,
       resource_uri, redirect_uri, created_at, updated_at
FROM mcp_oauth_tokens
WHERE state_param = $1
LIMIT 1
`

func (q *Queries) GetMCPOAuthTokenByState(ctx context.Context, stateParam string) (McpOauthToken, error) {
	row := q.db.QueryRow(ctx, getMCPOAuthTokenByState, stateParam)
	var i McpOauthToken
	err := row.Scan(
		&i.ID,
		&i.ConnectionID,
		&i.ResourceMetadataUrl,
		&i.AuthorizationServerUrl,
		&i.AuthorizationEndpoint,
		&i.TokenEndpoint,
		&i.RegistrationEndpoint,
		&i.ScopesSupported,
		&i.ClientID,
		&i.ClientSecret,
		&i.AccessToken,
		&i.RefreshToken,
		&i.TokenType,
		&i.ExpiresAt,
		&i.Scope,
		&i.PkceCodeVerifier,
		&i.StateParam,
		&i.ResourceUri,
		&i.RedirectUri,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const updateMCPOAuthClientSecret = `-- name: UpdateMCPOAuthClientSecret :exec
UPDATE mcp_oauth_tokens
SET client_secret = $2,
    updated_at = now()
WHERE connection_id = $1
`

type UpdateMCPOAuthClientSecretParams struct {
	ConnectionID pgtype.UUID `json:"connection_id"`
	ClientSecret string      `json:"client_secret"`
}

func (q *Queries) UpdateMCPOAuthClientSecret(ctx context.Context, arg UpdateMCPOAuthClientSecretParams) error {
	_, err := q.db.Exec(ctx, updateMCPOAuthClientSecret, arg.ConnectionID, arg.ClientSecret)
	return err
}

const updateMCPOAuthPKCEState = `-- name: UpdateMCPOAuthPKCEState :exec
UPDATE mcp_oauth_tokens
SET pkce_code_verifier = $2,
    state_param = $3,
    client_id = $4,
    redirect_uri = $5,
    updated_at = now()
WHERE connection_id = $1
`

type UpdateMCPOAuthPKCEStateParams struct {
	ConnectionID     pgtype.UUID `json:"connection_id"`
	PkceCodeVerifier string      `json:"pkce_code_verifier"`
	StateParam       string      `json:"state_param"`
	ClientID         string      `json:"client_id"`
	RedirectUri      string      `json:"redirect_uri"`
}

func (q *Queries) UpdateMCPOAuthPKCEState(ctx context.Context, arg UpdateMCPOAuthPKCEStateParams) error {
	_, err := q.db.Exec(ctx, updateMCPOAuthPKCEState,
		arg.ConnectionID,
		arg.PkceCodeVerifier,
		arg.StateParam,
		arg.ClientID,
		arg.RedirectUri,
	)
	return err
}

const updateMCPOAuthTokens = `-- name: UpdateMCPOAuthTokens :exec
UPDATE mcp_oauth_tokens
SET access_token = $2,
    refresh_token = $3,
    token_type = $4,
    expires_at = $5,
    scope = $6,
    pkce_code_verifier = '',
    state_param = '',
    updated_at = now()
WHERE connection_id = $1
`

type UpdateMCPOAuthTokensParams struct {
	ConnectionID pgtype.UUID        `json:"connection_id"`
	AccessToken  string             `json:"access_token"`
	RefreshToken string             `json:"refresh_token"`
	TokenType    string             `json:"token_type"`
	ExpiresAt    pgtype.Timestamptz `json:"expires_at"`
	Scope        string             `json:"scope"`
}

func (q *Queries) UpdateMCPOAuthTokens(ctx context.Context, arg UpdateMCPOAuthTokensParams) error {
	_, err := q.db.Exec(ctx, updateMCPOAuthTokens,
		arg.ConnectionID,
		arg.AccessToken,
		arg.RefreshToken,
		arg.TokenType,
		arg.ExpiresAt,
		arg.Scope,
	)
	return err
}

const upsertMCPOAuthDiscovery = `-- name: UpsertMCPOAuthDiscovery :one
INSERT INTO mcp_oauth_tokens (connection_id, resource_metadata_url, authorization_server_url,
    authorization_endpoint, token_endpoint, registration_endpoint, scopes_supported,
    resource_uri)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (connection_id)
DO UPDATE SET resource_metadata_url = EXCLUDED.resource_metadata_url,
              authorization_server_url = EXCLUDED.authorization_server_url,
              authorization_endpoint = EXCLUDED.authorization_endpoint,
              token_endpoint = EXCLUDED.token_endpoint,
              registration_endpoint = EXCLUDED.registration_endpoint,
              scopes_supported = EXCLUDED.scopes_supported,
              resource_uri = EXCLUDED.resource_uri,
              updated_at = now()
RETURNING id, connection_id, resource_metadata_url, authorization_server_url,
          authorization_endpoint, token_endpoint, registration_endpoint,
          scopes_supported, client_id, client_secret, access_token, refresh_token,
          token_type, expires_at, scope, pkce_code_verifier, state_param,
          resource_uri, redirect_uri, created_at, updated_at
`

type UpsertMCPOAuthDiscoveryParams struct {
	ConnectionID           pgtype.UUID `json:"connection_id"`
	ResourceMetadataUrl    string      `json:"resource_metadata_url"`
	AuthorizationServerUrl string      `json:"authorization_server_url"`
	AuthorizationEndpoint  string      `json:"authorization_endpoint"`
	TokenEndpoint          string      `json:"token_endpoint"`
	RegistrationEndpoint   string      `json:"registration_endpoint"`
	ScopesSupported        []string    `json:"scopes_supported"`
	ResourceUri            string      `json:"resource_uri"`
}

func (q *Queries) UpsertMCPOAuthDiscovery(ctx context.Context, arg UpsertMCPOAuthDiscoveryParams) (McpOauthToken, error) {
	row := q.db.QueryRow(ctx, upsertMCPOAuthDiscovery,
		arg.ConnectionID,
		arg.ResourceMetadataUrl,
		arg.AuthorizationServerUrl,
		arg.AuthorizationEndpoint,
		arg.TokenEndpoint,
		arg.RegistrationEndpoint,
		arg.ScopesSupported,
		arg.ResourceUri,
	)
	var i McpOauthToken
	err := row.Scan(
		&i.ID,
		&i.ConnectionID,
		&i.ResourceMetadataUrl,
		&i.AuthorizationServerUrl,
		&i.AuthorizationEndpoint,
		&i.TokenEndpoint,
		&i.RegistrationEndpoint,
		&i.ScopesSupported,
		&i.ClientID,
		&i.ClientSecret,
		&i.AccessToken,
		&i.RefreshToken,
		&i.TokenType,
		&i.ExpiresAt,
		&i.Scope,
		&i.PkceCodeVerifier,
		&i.StateParam,
		&i.ResourceUri,
		&i.RedirectUri,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
