package handler

import (
	"context"
	"database/sql"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"regexp"
	"strconv"
	"strings"
	"time"
	"vctp/db/queries"
	"vctp/internal/vcenter"
	models "vctp/server/models"

	"github.com/vmware/govmomi/vim25/types"
)

// VmModify receives the CloudEvent for a VM modification or move
func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
	var configChanges []map[string]string
	params := queries.CreateUpdateParams{}
	var unixTimestamp int64

	re := regexp.MustCompile(`/([^/]+)/[^/]+\.vmdk$`)

	reqBody, err := io.ReadAll(r.Body)
	if err != nil {
		h.Logger.Error("Invalid data received", "error", err)
		fmt.Fprintf(w, "Invalid data received")
		w.WriteHeader(http.StatusInternalServerError)
		return
	} else {
		h.Logger.Debug("received input data", "length", len(reqBody))
	}

	// Decode the JSON body into CloudEventReceived struct
	var event models.CloudEventReceived
	if err := json.Unmarshal(reqBody, &event); err != nil {
		h.Logger.Error("unable to decode json", "error", err)
		prettyPrint(reqBody)
		http.Error(w, "Invalid JSON body", http.StatusBadRequest)
		return
	} else {
		h.Logger.Debug("successfully decoded JSON")
		//prettyPrint(event)
	}

	if event.CloudEvent.Data.ConfigChanges == nil {
		h.Logger.Warn("Received event contains no config change")
		prettyPrint(event)
		w.WriteHeader(http.StatusAccepted)
		fmt.Fprintf(w, "Processed update event but no config changes were found\n")
	} else {
		h.Logger.Debug("Received event contains config change info", "source", event.CloudEvent.Source,
			"vm", event.CloudEvent.Data.VM.Name, "user_name", event.CloudEvent.Data.UserName)

		// Try to decode the config changes data
		var testConfig models.ConfigSpec
		if err := json.Unmarshal(*event.CloudEvent.Data.ConfigSpec, &testConfig); err != nil {
			h.Logger.Warn("unable to decode ConfigSpec json", "error", err)
		} else {
			h.Logger.Debug("successfully decoded ConfigSpec JSON")
		}

		configChanges = h.processConfigChanges(event.CloudEvent.Data.ConfigChanges.Modified)
		//prettyPrint(configChanges)

		var found = false
		// Only interested in vCPU or ram changes currently
		for _, change := range configChanges {
			//fmt.Printf("Type: %s, New Value: %s\n", change["type"], change["newValue"])
			switch change["type"] {
			case "config.hardware.numCPU":
				i, err := strconv.ParseInt(change["newValue"], 10, 64)
				if err != nil {
					h.Logger.Error("Unable to convert new value to int64", "new_value", change["newValue"])
				} else {
					found = true
					params.NewVcpus = sql.NullInt64{Int64: i, Valid: i > 0}
					params.UpdateType = "reconfigure"
				}
			case "config.hardware.memoryMB":
				i, err := strconv.ParseInt(change["newValue"], 10, 64)
				if err != nil {
					h.Logger.Error("Unable to convert new value to int64", "new_value", change["newValue"])
				} else {
					found = true
					params.NewRam = sql.NullInt64{Int64: i, Valid: i > 0}
					params.UpdateType = "reconfigure"
				}
			}

			// Check if a disk was added (or maybe removed?)
			if strings.Contains(change["type"], "config.hardware.device") &&
				strings.Contains(event.CloudEvent.Data.FullFormattedMessage, ".vmdk") {

				// TODO - query current disk size from Inventory table and only create an update if the size is now changed
				if testConfig.DeviceChange != nil {
					for i := range testConfig.DeviceChange {
						if testConfig.DeviceChange[i].Device.Backing != nil {
							h.Logger.Debug("Found backing in configspec", "backing", testConfig.DeviceChange[i].Device.Backing)

							// Find the match
							backingFile := testConfig.DeviceChange[i].Device.Backing.FileName
							matches := re.FindStringSubmatch(backingFile)
							if len(matches) < 2 {
								h.Logger.Warn("unable to match regex", "backing_filename", backingFile, "match_count", len(matches))
							} else {
								h.Logger.Debug("Matched regex", "disk_owner", matches[1])

								if strings.ToLower(matches[1]) == strings.ToLower(event.CloudEvent.Data.VM.Name) {
									h.Logger.Debug("This disk belongs to this VM")
									found = true
									params.UpdateType = "diskchange"
									diskSize := h.calculateNewDiskSize(event)
									params.NewProvisionedDisk = sql.NullFloat64{Float64: diskSize, Valid: diskSize > 0}

								} else {
									h.Logger.Debug("This disk belongs to a different VM, don't record this config change")
								}
							}
						}
					}
				}

				// TODO - recalculate total disk size
				//h.Logger.Debug("Detected config change for VM disk")

			}
		}

		// Only create a database record if we found one of the config changes we were interested in
		if found {
			// lookup Iid from Inventory table for this VM
			// also figure out what to do if we didn't find an entry for this VM in the Inventory table. Create one?
			h.Logger.Debug("Checking inventory table for VM record")
			invParams := queries.GetInventoryVmIdParams{
				VmId:           sql.NullString{String: event.CloudEvent.Data.VM.VM.Value, Valid: event.CloudEvent.Data.VM.VM.Value != ""},
				DatacenterName: sql.NullString{String: event.CloudEvent.Data.Datacenter.Name, Valid: event.CloudEvent.Data.Datacenter.Name != ""},
			}
			invResult, err := h.Database.Queries().GetInventoryVmId(context.Background(), invParams)

			if err != nil {
				h.Logger.Error("unable to find existing inventory record for this VM", "error", err)
				// TODO - how to handle?
			} else {
				params.InventoryId = sql.NullInt64{Int64: invResult.Iid, Valid: invResult.Iid > 0}
			}

			// Parse the datetime string to a time.Time object
			eventTime, err := time.Parse(time.RFC3339, event.CloudEvent.Data.CreatedTime)
			if err != nil {
				h.Logger.Warn("unable to convert cloud event time to timestamp", "error", err)
				unixTimestamp = time.Now().Unix()
			} else {
				// Convert to Unix timestamp
				unixTimestamp = eventTime.Unix()
			}

			// populate other parameters for the Update database record
			params.EventId = sql.NullString{String: event.CloudEvent.ID, Valid: event.CloudEvent.ID != ""}
			params.EventKey = sql.NullString{String: strconv.Itoa(event.CloudEvent.Data.Key), Valid: event.CloudEvent.Data.Key > 0}
			params.UpdateTime = sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0}

			// Create the Update database record
			result, err := h.Database.Queries().CreateUpdate(context.Background(), params)
			if err != nil {
				h.Logger.Error("unable to perform database insert", "error", err)
				w.WriteHeader(http.StatusInternalServerError)
				fmt.Fprintf(w, "Error : %v\n", err)
				return
			} else {
				h.Logger.Debug("created database record", "insert_result", result)
				w.WriteHeader(http.StatusOK)
				fmt.Fprintf(w, "Processed update event: %v\n", result)
				return
			}
		} else {
			w.WriteHeader(http.StatusAccepted)
			fmt.Fprintf(w, "Processed update event but no config changes were of interest\n")
			prettyPrint(event.CloudEvent.Data.ConfigSpec)
		}
	}
}

func (h *Handler) processConfigChanges(configChanges string) []map[string]string {
	// Split the string on one or more consecutive newline characters
	changes := regexp.MustCompile(`\n+`).Split(configChanges, -1)

	// Regular expression to match config type and the new value after '->' or '<-'
	// examples:
	// "config.memoryHotAddEnabled: true -\u003e false; \n\nconfig.cpuHotAddEnabled: true -\u003e false; \n\n"
	// "config.hardware.device(1000).device: (2000, 2001, 2002) -> (2000, 2001, 2002, 2003);"
	// "config.hardware.numCPU: 2 -\u003e 1; \n\nconfig.hardware.memoryMB: 4096 -\u003e 3072;"
	re := regexp.MustCompile(`(?P<type>[^\s]+): [^-]+-[><] (?P<newValue>[^;]+);`)

	// Result will hold a list of changes with type and new value
	var result []map[string]string

	for _, change := range changes {
		// Trim any extra spaces and skip empty lines
		change = strings.TrimSpace(change)
		//h.Logger.Debug("Processing config change element", "substring", change)
		if change == "" {
			continue
		}

		// Find the matches using the regex
		match := re.FindStringSubmatch(change)
		if len(match) > 0 {
			// Create a map with 'type' and 'newValue'
			changeMap := map[string]string{
				"type":     match[1], // config type
				"newValue": match[2], // new value after -> or <-
			}
			//h.Logger.Debug("Adding new entry to output", "map", changeMap)
			result = append(result, changeMap)
		} else {
			h.Logger.Warn("No regex matches for string", "input", change)
		}
	}

	return result
}

func (h *Handler) calculateNewDiskSize(event models.CloudEventReceived) float64 {
	var diskSize float64
	h.Logger.Debug("connecting to vcenter")
	vc := vcenter.New(h.Logger)
	vc.Login(event.CloudEvent.Source)

	vmObject, err := vc.FindVMByIDWithDatacenter(event.CloudEvent.Data.VM.VM.Value, event.CloudEvent.Data.Datacenter.Datacenter.Value)

	if err != nil {
		h.Logger.Error("Can't locate vm in vCenter", "vmID", event.CloudEvent.Data.VM.VM.Value, "error", err)
	} else {
		if vmObject.Vm.Config != nil {
			h.Logger.Debug("Found VM with config, calculating new total disk size", "vmID", event.CloudEvent.Data.VM.VM.Value)
			// Calculate the total disk allocated in GB
			for _, device := range vmObject.Vm.Config.Hardware.Device {
				if disk, ok := device.(*types.VirtualDisk); ok {

					// Print the filename of the backing device
					if backing, ok := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo); ok {
						h.Logger.Debug("Adding disk", "size_bytes", disk.CapacityInBytes, "backing_file", backing.FileName)
					} else {
						h.Logger.Debug("Adding disk, unknown backing type", "size_bytes", disk.CapacityInBytes)
					}

					diskSize += float64(disk.CapacityInBytes / 1024 / 1024 / 1024) // Convert from bytes to GB
				}
			}
		}
	}

	err = vc.Logout()
	if err != nil {
		h.Logger.Error("unable to logout of vcenter", "error", err)
	}

	h.Logger.Debug("Calculated new disk size", "value", diskSize)

	return diskSize
}
