package main

import (
	"context"
	"fmt"
	"os"

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

func main() {
	dbURL := "postgres://user:pass@localhost:54322/photum-v2?sslmode=disable"
	conn, err := pgx.Connect(context.Background(), dbURL)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
		os.Exit(1)
	}
	defer conn.Close(context.Background())

	rows, err := conn.Query(context.Background(), "SELECT id, ano_semestre, regiao FROM anos_formaturas ORDER BY ano_semestre, regiao")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Query failed: %v\n", err)
		os.Exit(1)
	}
	defer rows.Close()

	fmt.Println("--- ANOS FORMATURAS DUMP ---")
	fmt.Printf("%-36s | %-10s | %-5s\n", "ID", "Ano", "Reg")
	for rows.Next() {
		var id string
		var ano string
		var reg *string
		err := rows.Scan(&id, &ano, &reg)
		if err != nil {
			fmt.Printf("Scan error: %v\n", err)
			continue
		}
		regVal := "NULL"
		if reg != nil {
			regVal = *reg
		}
		fmt.Printf("%-36s | %-10s | %-5s\n", id, ano, regVal)
	}
	fmt.Println("----------------------------")
}
