// Code generated by cmd/gen/sqlc-wrapper. DO NOT EDIT.
package postgres

import (
	"context"
	"database/sql"
	"errors"

	"github.com/tinyauthapp/tinyauth/internal/repository"
)

// Store wraps *Queries and implements repository.Store.
type Store struct {
	q *Queries
}

// NewStore wraps a *Queries to satisfy repository.Store.
func NewStore(q *Queries) repository.Store {
	return &Store{q: q}
}

var errorMap = map[error]error{
	sql.ErrNoRows: repository.ErrNotFound,
}

func mapErr(err error) error {
	for from, to := range errorMap {
		if errors.Is(err, from) {
			return to
		}
	}
	return err
}

func (s *Store) CreateOIDCSession(ctx context.Context, arg repository.CreateOIDCSessionParams) (repository.OidcSession, error) {
	r, err := s.q.CreateOIDCSession(ctx, CreateOIDCSessionParams(arg))
	if err != nil {
		return repository.OidcSession{}, mapErr(err)
	}
	return repository.OidcSession(r), nil
}

func (s *Store) CreateSession(ctx context.Context, arg repository.CreateSessionParams) (repository.Session, error) {
	r, err := s.q.CreateSession(ctx, CreateSessionParams(arg))
	if err != nil {
		return repository.Session{}, mapErr(err)
	}
	return repository.Session(r), nil
}

func (s *Store) DeleteExpiredOIDCSessions(ctx context.Context, arg repository.DeleteExpiredOIDCSessionsParams) error {
	return mapErr(s.q.DeleteExpiredOIDCSessions(ctx, DeleteExpiredOIDCSessionsParams(arg)))
}

func (s *Store) DeleteExpiredSessions(ctx context.Context, expiry int64) error {
	return mapErr(s.q.DeleteExpiredSessions(ctx, expiry))
}

func (s *Store) DeleteOIDCConsentByClientID(ctx context.Context, clientID string) error {
	return mapErr(s.q.DeleteOIDCConsentByClientID(ctx, clientID))
}

func (s *Store) DeleteOIDCSessionBySub(ctx context.Context, sub string) error {
	return mapErr(s.q.DeleteOIDCSessionBySub(ctx, sub))
}

func (s *Store) DeleteSession(ctx context.Context, uuid string) error {
	return mapErr(s.q.DeleteSession(ctx, uuid))
}

func (s *Store) GetOIDCConsentByUsernameAndClientID(ctx context.Context, arg repository.GetOIDCConsentByUsernameAndClientIDParams) (repository.OidcConsent, error) {
	r, err := s.q.GetOIDCConsentByUsernameAndClientID(ctx, GetOIDCConsentByUsernameAndClientIDParams(arg))
	if err != nil {
		return repository.OidcConsent{}, mapErr(err)
	}
	return repository.OidcConsent(r), nil
}

func (s *Store) GetOIDCSessionByAccessTokenHash(ctx context.Context, accessTokenHash string) (repository.OidcSession, error) {
	r, err := s.q.GetOIDCSessionByAccessTokenHash(ctx, accessTokenHash)
	if err != nil {
		return repository.OidcSession{}, mapErr(err)
	}
	return repository.OidcSession(r), nil
}

func (s *Store) GetOIDCSessionByRefreshTokenHash(ctx context.Context, refreshTokenHash string) (repository.OidcSession, error) {
	r, err := s.q.GetOIDCSessionByRefreshTokenHash(ctx, refreshTokenHash)
	if err != nil {
		return repository.OidcSession{}, mapErr(err)
	}
	return repository.OidcSession(r), nil
}

func (s *Store) GetOIDCSessionBySub(ctx context.Context, sub string) (repository.OidcSession, error) {
	r, err := s.q.GetOIDCSessionBySub(ctx, sub)
	if err != nil {
		return repository.OidcSession{}, mapErr(err)
	}
	return repository.OidcSession(r), nil
}

func (s *Store) GetSession(ctx context.Context, uuid string) (repository.Session, error) {
	r, err := s.q.GetSession(ctx, uuid)
	if err != nil {
		return repository.Session{}, mapErr(err)
	}
	return repository.Session(r), nil
}

func (s *Store) ListOIDCConsents(ctx context.Context) ([]repository.OidcConsent, error) {
	rows, err := s.q.ListOIDCConsents(ctx)
	if err != nil {
		return nil, mapErr(err)
	}
	out := make([]repository.OidcConsent, len(rows))
	for i, row := range rows {
		out[i] = repository.OidcConsent(row)
	}
	return out, nil
}

func (s *Store) UpdateOIDCSession(ctx context.Context, arg repository.UpdateOIDCSessionParams) (repository.OidcSession, error) {
	r, err := s.q.UpdateOIDCSession(ctx, UpdateOIDCSessionParams(arg))
	if err != nil {
		return repository.OidcSession{}, mapErr(err)
	}
	return repository.OidcSession(r), nil
}

func (s *Store) UpdateSession(ctx context.Context, arg repository.UpdateSessionParams) (repository.Session, error) {
	r, err := s.q.UpdateSession(ctx, UpdateSessionParams(arg))
	if err != nil {
		return repository.Session{}, mapErr(err)
	}
	return repository.Session(r), nil
}

func (s *Store) UpsertOIDCConsent(ctx context.Context, arg repository.UpsertOIDCConsentParams) (repository.OidcConsent, error) {
	r, err := s.q.UpsertOIDCConsent(ctx, UpsertOIDCConsentParams(arg))
	if err != nil {
		return repository.OidcConsent{}, mapErr(err)
	}
	return repository.OidcConsent(r), nil
}
