package sample

import (
	"database/sql"
	"net/http"

	"entgo.io/ent"
	"github.com/jackc/pgx/v5/pgxpool"
	redis "github.com/redis/go-redis/v9"
	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/pgdialect"
)

type Server struct{}

var sharedPool *pgxpool.Pool
var sharedRedis = redis.NewClient(&redis.Options{Addr: "localhost:6379"})
var sharedBun *bun.DB
var sharedEnt *ent.Client

func initStore(db *sql.DB) {
	sharedBun = bun.NewDB(db, pgdialect.New())
	sharedEnt, _ = ent.Open("postgres", "postgres://demo")
}

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	keys := []string{"a", "b"}
	pipe := sharedRedis.Pipeline()
	for _, key := range keys {
		_ = pipe.Get(ctx, key)
	}
	_, _ = pipe.Exec(ctx)

	rows := make([]struct{ ID int }, 0, 32)
	_ = sharedBun.NewSelect().Model(&rows).Limit(50).Scan(ctx)
	_ = sharedPool
	_ = sharedEnt
	w.WriteHeader(http.StatusAccepted)
}
