package main

import (
	"context"
	"log"

	_ "github.com/jackc/pgx/v5/stdlib"
	"github.com/jmoiron/sqlx"
	"golang.org/x/crypto/bcrypt"

	"github.com/saveinmed/backend-go/internal/config"
)

func main() {
	cfg, err := config.Load()
	if err != nil {
		log.Fatalf("failed to load config: %v", err)
	}

	db, err := sqlx.Open("pgx", cfg.DatabaseURL)
	if err != nil {
		log.Fatalf("Failed to connect to DB: %v", err)
	}
	defer db.Close()

	if err := db.Ping(); err != nil {
		log.Fatalf("Failed to ping DB: %v", err)
	}

	ctx := context.Background()

	// Nova senha: senha123
	newPassword := "senha123"

	// Hash da senha com bcrypt
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword+cfg.PasswordPepper), bcrypt.DefaultCost)
	if err != nil {
		log.Fatalf("Failed to hash password: %v", err)
	}

	// Atualizar senha do usuário
	query := `UPDATE users SET password_hash = $1 WHERE email = $2`
	result, err := db.ExecContext(ctx, query, string(hashedPassword), "usuario@saveinmed.com")
	if err != nil {
		log.Fatalf("Failed to update password: %v", err)
	}

	rowsAffected, _ := result.RowsAffected()
	if rowsAffected == 0 {
		log.Fatalf("User not found: usuario@saveinmed.com")
	}

	log.Println("✅ Password reset successful!")
	log.Println("📧 Email: usuario@saveinmed.com")
	log.Println("🔑 New Password: senha123")
}
