//go:build ignore

package main

import (
	"database/sql"
	"fmt"
	"log"

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

func main() {
	dsn := "host=66.116.207.225 port=5433 user=admin password=Package@123# dbname=nearledb sslmode=disable"
	db, err := sql.Open("pgx", dsn)
	if err != nil {
		log.Fatalf("Error opening db connection: %v", err)
	}
	defer db.Close()

	err = db.Ping()
	if err != nil {
		log.Fatalf("Error pinging db: %v", err)
	}
	fmt.Println("Connected successfully to PostgreSQL!")

	// 1. Create a temp user for delete testing
	authName := "temp.delete.test@suriya.com"
	_, err = db.Exec(`
		INSERT INTO app_users (
			authname, firstname, lastname, password, email, dialcode, contactno,
			configid, authmode, roleid, pin, deviceid, devicetype, userfcmtoken,
			address, suburb, city, state, postcode, partnerid, tenantid, locationid,
			applocationid, status, shiftid
		) VALUES (
			$1, 'Temp', 'Delete Test', 'Password123', $1, '+91', '9999999999',
			1, 0, 0, 0, '', '', '',
			'', '', '', '', '', 0, 1135, 1166,
			1, 'Active', 0
		)
	`, authName)
	if err != nil {
		log.Fatalf("Error creating temp user: %v", err)
	}
	fmt.Println("Temp user created.")

	// 2. Fetch the created user ID
	var userID int
	err = db.QueryRow("SELECT userid FROM app_users WHERE authname = $1", authName).Scan(&userID)
	if err != nil {
		log.Fatalf("Error fetching user ID: %v", err)
	}
	fmt.Printf("Temp user ID is: %d\n", userID)

	// 3. Delete the user
	_, err = db.Exec("DELETE FROM app_users WHERE userid = $1", userID)
	if err != nil {
		log.Fatalf("Error deleting user: %v", err)
	}
	fmt.Println("Temp user deleted successfully.")

	// 4. Verify no longer exists
	var checkID int
	err = db.QueryRow("SELECT userid FROM app_users WHERE userid = $1", userID).Scan(&checkID)
	if err == sql.ErrNoRows {
		fmt.Println("Verification Success: User no longer exists in DB!")
	} else if err != nil {
		log.Fatalf("Query check error: %v", err)
	} else {
		log.Fatalf("Error: User still exists in database!")
	}
}
