package main

import (
	"context"
	"fmt"
	"os"

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

func main() {
	ctx := context.Background()
	connStr := os.Getenv("MIP_DB")
	if connStr == "" {
		connStr = "postgres://user:password@localhost:5432/mydatabase?sslmode=disable"
	}

	conn, err := pgx.Connect(ctx, connStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to connect: %v\n", err)
		os.Exit(1)
	}
	defer conn.Close(ctx)

	var count int
	err = conn.QueryRow(ctx, "SELECT COUNT(*) FROM shock_brand_specs").Scan(&count)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Query failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Total: %d\n", count)

	rows, _ := conn.Query(ctx, "SELECT sb.brand, COUNT(ss.id) FROM shock_brand_specs ss JOIN shock_brands sb ON ss.\"fk_idShockBrand\" = sb.id GROUP BY sb.brand")
	defer rows.Close()
	for rows.Next() {
		var brand string
		var c int
		rows.Scan(&brand, &c)
		fmt.Printf("%s: %d\n", brand, c)
	}
}