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())

	// Check Empresas
	fmt.Println("Checking EMPRESAS duplicates...")
	rows, err := conn.Query(context.Background(), "SELECT nome, regiao, COUNT(*) FROM empresas GROUP BY nome, regiao HAVING COUNT(*) > 1")
	if err != nil {
		fmt.Printf("Query error: %v\n", err)
	} else {
		defer rows.Close()
		count := 0
		for rows.Next() {
			var nome, reg string
			var c int
			rows.Scan(&nome, &reg, &c)
			fmt.Printf("DUPLICATE: %s in %s (Count: %d)\n", nome, reg, c)
			count++
		}
		if count == 0 {
			fmt.Println("No duplicates found in empresas.")
		}
	}

	// Check Anos
	fmt.Println("\nChecking ANOS duplicates...")
	rows2, err := conn.Query(context.Background(), "SELECT ano_semestre, regiao, COUNT(*) FROM anos_formaturas GROUP BY ano_semestre, regiao HAVING COUNT(*) > 1")
	if err != nil {
		fmt.Printf("Query error: %v\n", err)
	} else {
		defer rows2.Close()
		count := 0
		for rows2.Next() {
			var nome, reg string
			var c int
			rows2.Scan(&nome, &reg, &c)
			fmt.Printf("DUPLICATE: %s in %s (Count: %d)\n", nome, reg, c)
			count++
		}
		if count == 0 {
			fmt.Println("No duplicates found in anos_formaturas.")
		}
	}
}
