package db

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

type rowScanner interface {
	Scan(dest ...any) error
}

func formatTimestamp(value time.Time) string {
	return value.UTC().Format(time.RFC3339)
}

func parseTimestamp(raw string) (time.Time, error) {
	parsed, err := time.Parse(time.RFC3339, raw)
	if err != nil {
		return time.Time{}, fmt.Errorf("parse timestamp %q: %w", raw, err)
	}

	return parsed.UTC(), nil
}

func parseNullableTimestamp(raw sql.NullString) (*time.Time, error) {
	if !raw.Valid || strings.TrimSpace(raw.String) == "" {
		return nil, nil
	}

	parsed, err := parseTimestamp(raw.String)
	if err != nil {
		return nil, err
	}

	return &parsed, nil
}
