// Package db provides a MySQL connection helper.
package db

import (
	"database/sql"
	"fmt"
	"time"

	// Register mysql driver.
	_ "github.com/go-sql-driver/mysql"
)

// Open opens a *sql.DB backed by MySQL and pings the server.
// The DSN must include parseTime=true and time_zone='+00:00' (UTC).
func Open(dsn string) (*sql.DB, error) {
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		return nil, fmt.Errorf("db.Open: %w", err)
	}
	db.SetMaxOpenConns(30)
	db.SetMaxIdleConns(10)
	db.SetConnMaxLifetime(5 * time.Minute)

	if err := db.Ping(); err != nil {
		db.Close()
		return nil, fmt.Errorf("db.Open ping: %w", err)
	}
	return db, nil
}
