package main

import (
	"database/sql"
	"fmt"
	"os"
	"log"
	"net/http"
	"io/ioutil"
	"encoding/json"

	"github.com/joho/godotenv"

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

func main() {
	// Load environment variables from .env file
	err := godotenv.Load()
	if err != nil {
		log.Fatal("Error loading .env file")
	}
	//Connect to the database
	db, err := sql.Open("mysql", os.Getenv("DB_USER")+":"+os.Getenv("DB_PASS")+"@tcp("+os.Getenv("DB_HOST")+":"+os.Getenv("DB_PORT")+")/"+os.Getenv("DATABASE"))
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Verify that the connection is successful
	err = db.Ping()
	if err != nil {
		log.Fatal(err)
	}

	// Perform a cleanup on DB
	deleter, err := db.Exec("DELETE FROM phish;")
	if err != nil {
		log.Fatal(err)
	}
	// Get the number of affected rows
	rowsAffected, err := deleter.RowsAffected()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Number of rows affected: %d\n", rowsAffected)

	// Perform a cleanup on DB
	aiupdate, err := db.Exec("ALTER TABLE phish AUTO_INCREMENT = 0;")
	if err != nil {
		log.Fatal(err)
	}
	// Get the number of affected rows
	rowsAffected, err = aiupdate.RowsAffected()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Number of rows affected: %d\n", rowsAffected)

	// Add local records first
	insert1, err := db.Exec("INSERT INTO phish (Domain, Reporter, Evidence) VALUES ('iriseperiplo.info', 'Henk', 'https://www.virustotal.com/gui/url/318ac29af0001f6678efdd94c16d3225d78369123ebbbe5f50387b208e0ac523?nocache=1');")
	insert2, err2 := db.Exec("INSERT INTO phish (Domain, Reporter, Evidence) VALUES ('storage.com', 'Henk', 'https://www.virustotal.com/gui/url/92fef9276cd4c0433171aac2fe92d140df069ff99d724c850ca1d3ccbdd0ae9f?nocache=1');")
	if err != nil {
		log.Fatal(err)
	}
	if err2 != nil {
		log.Fatal(err)
	}
	// Get the number of affected rows
	rowsAffected, err = insert1.RowsAffected()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Number of rows affected: %d\n", rowsAffected)
	// Get the number of affected rows
	rowsAffected, err = insert2.RowsAffected()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Number of rows affected: %d\n", rowsAffected)

	// Define name for use later
	// name := os.Getenv("USER_OR_BOTNAME")

	// Make an HTTP request to phish.sinking.yachts alldomains endpoint
	req, err := http.NewRequest("GET", "https://phish.sinking.yachts/v2/all", nil)
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("X-Identity", "ultimatebot db updater via Golang")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	fmt.Println("Status code: ", resp.StatusCode)
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	var data []string
	err = json.Unmarshal(body, &data)
	if err != nil {
		log.Fatal(err)
	}

	// Initialize a variable to keep track of the total number of rows affected
	var totalRowsAffected int64

	for _, element := range data {
		// fmt.Println(element)
		// Add domains
		inserter, err := db.Exec("INSERT INTO phish (Domain) VALUES (?)", element)
		if err != nil {
			log.Fatal(err)
		}

		rowsAffected, err := inserter.RowsAffected()
		if err != nil {
			log.Fatal(err)
		}

		totalRowsAffected += rowsAffected
	}
	fmt.Printf("Total number of rows affected: %d\n", totalRowsAffected)
	// dummy
	// fmt.Println("Data: ", string(body))
}