package controllers

import (
	"context"
	"encoding/json"
	"log"
	"math/big"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"gitlab.com/pactual1/backend/config"
	"gitlab.com/pactual1/backend/database/contract"
	"gitlab.com/pactual1/backend/database/device"
	"gitlab.com/pactual1/backend/models"
	"gitlab.com/pactual1/backend/services/blockchain"
	"gitlab.com/pactual1/backend/shared"
)

const (
	ContractDistanceThresholdKm = float64(10)
)

func SaveDeviceInfo(c *gin.Context) {
	var deviceInfo models.DeviceInfo
	rawData, _ := c.GetRawData()

	// Unmarshal to the important info structure
	err := json.Unmarshal(rawData, &deviceInfo)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"})
		log.Printf("Invalid json payload : %v", err)
		return
	}

	deviceInfo, currentDevice, err := device.SaveDeviceInfoToDB(deviceInfo, rawData)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	if currentDevice.CurrentContractID != nil {
		deviceContract, _, err := contract.GetContractByID(*currentDevice.CurrentContractID)
		if err != nil {
			log.Printf("SaveDeviceInfo - GetContractByID error : %v", err)
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not fetch device contract"})
			return
		}

		if deviceContract.Status == models.ContractStatusActive {
			deviceInfoBytes, _ := json.Marshal(deviceInfo)
			if deviceContract.BlockchainSecret == "" {
				deviceContract.BlockchainSecret = shared.GenerateRandomString(BlockchainSecretLength)
				deviceContract, _, err = contract.UpdateContract(deviceContract)
				if err != nil {
					log.Printf("SaveDeviceInfo Update Contract error: %v", err)
					c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not update contract secret"})
					return
				}
			}
			deviceInfoEncryptedStr, err := shared.NewEncryptionClient(deviceContract.BlockchainSecret).Encrypt(string(deviceInfoBytes))
			if err != nil {
				log.Printf("SaveDeviceInfo - Enrypt error : %v", err)
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not encrypt device info"})
				return
			}

			err = blockchain.NewService(config.AppConfig.Blockchain).AddIOTData(context.Background(), shared.CovertUintToByte32(deviceContract.ID), shared.CovertUintToByte32(currentDevice.ID), big.NewInt(time.Now().Unix()), []byte(deviceInfoEncryptedStr))
			if err != nil {
				log.Printf("SaveDeviceInfo CREATE -DeviceInfo Blockchain Error: %v", err)
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not save device info in blockchain"})
				return
			}

			if shared.DistanceKm(deviceInfo.Lat, deviceInfo.Lon, deviceContract.EndLat, deviceContract.EndLon) <= ContractDistanceThresholdKm {
				deviceContract.Status = models.ContractStatusExecuted
				_, _, err := contract.UpdateContract(deviceContract)
				if err != nil {
					log.Printf("SaveDeviceInfo Update Contract error: %v", err)
					c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not update contract status"})
					return
				}
			}
		}
	}

	log.Printf("Successfully received and saved device info: %v", deviceInfo)
	c.JSON(http.StatusOK, gin.H{"message": "Successfully received and saved device info", "data": deviceInfo})
}

func GetDeviceData(c *gin.Context) {
	// Get the device ID and contract ID from query parameters
	deviceIDStr := c.DefaultQuery("device_id", "")
	contractIDStr := c.DefaultQuery("contract_id", "")

	if deviceIDStr == "" {
		log.Printf("GetDeviceData Error: Device ID is required")
		c.JSON(http.StatusBadRequest, gin.H{"error": "Device ID is required"})
		return
	}

	deviceID, err := strconv.ParseUint(deviceIDStr, 10, 32)
	if err != nil {
		log.Printf("GetDeviceData Error: Invalid Device ID: %v", err)
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Device ID"})
		return
	}

	contractID, err := strconv.ParseUint(contractIDStr, 10, 32)
	if err != nil {
		log.Printf("GetDeviceData Error: Invalid Contract ID: %v", err)
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Contract ID"})
		return
	}

	contract, st, err := contract.GetContractByID(uint(contractID))
	if err != nil {
		c.JSON(st, gin.H{"error": err.Error()})
		return
	}

	deviceConnectedToContract := false
	for _, contractDeviceID := range contract.DeviceIDs {
		if deviceID == uint64(contractDeviceID) {
			deviceConnectedToContract = true
		}
	}

	if !deviceConnectedToContract {
		log.Printf("Device %v is not connected to contract %v", deviceID, contractID)
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Device is not present int his contract"})
		return
	}

	featureCollection, st, err := device.GetDeviceInfoForContract(deviceID, contract)

	if err != nil {
		c.JSON(st, gin.H{"error": err.Error()})
		return
	}

	// Respond with the GeoJSON feature collection
	c.JSON(http.StatusOK, gin.H{"data": featureCollection})
}

func GetDevicesByContract(c *gin.Context) {
	// Get the contract ID from query parameter
	contractIDStr := c.DefaultQuery("contract_id", "")
	if contractIDStr == "" {
		log.Printf("GetDevicesByContract Error: Contract ID is required")
		c.JSON(http.StatusBadRequest, gin.H{"error": "Contract ID is required"})
		return
	}
	companyID := c.GetInt("companyID")

	// Convert string to uint
	contractID, err := strconv.ParseUint(contractIDStr, 10, 32)
	if err != nil {
		log.Printf("GetDevicesByContract Error: Invalid Contract ID: %v", err)
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Contract ID"})
		return
	}

	log.Printf("This is the  ID: %v", contractID)
	devices, st, err := device.GetDevicesForContract(contractID, companyID)

	if err != nil {
		c.JSON(st, gin.H{"error": err.Error()})
		return
	}
	// Respond with the devices
	c.JSON(http.StatusOK, gin.H{"data": models.ConvertDeviceToResponse(devices)})
}

func GetCompanyRelatedDeviceInfoCount(c *gin.Context) {
	// Get the Company ID from query parameters
	companyIDStr := c.DefaultQuery("company_id", "")
	if companyIDStr == "" {
		log.Printf("GetCompanyRelatedDeviceInfoCount Error: Company ID is required")
		c.JSON(http.StatusBadRequest, gin.H{"error": "Company ID is required"})
		return
	}

	// Convert string to uint for CompanyID
	companyID, err := strconv.ParseUint(companyIDStr, 10, 32)
	if err != nil {
		log.Printf("GetCompanyRelatedDeviceInfoCount Error: Invalid Company ID: %v", err)
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Company ID"})
		return
	}

	// Perform the counting
	count, err := device.CountDeviceInfoByCompany(uint(companyID))
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	// Respond with the count
	c.JSON(http.StatusOK, gin.H{"data": count})
}

func GetCompanyRelatedDeviceInfoCountWithTempRange(c *gin.Context) {
	// Get the Company ID from query parameters
	companyIDStr := c.DefaultQuery("company_id", "")
	startTimeStr := c.DefaultQuery("start_time", "")
	endTimeStr := c.DefaultQuery("end_time", "")
	if companyIDStr == "" {
		log.Printf("GetCompanyRelatedDeviceInfoCount Error: Company ID is required")
		c.JSON(http.StatusBadRequest, gin.H{"error": "Company ID is required"})
		return
	}
	if startTimeStr == "" {
		log.Printf("GetCompanyRelatedDeviceInfoCount Error: startTime ID is required")
		c.JSON(http.StatusBadRequest, gin.H{"error": "Start time is required"})
		return
	}

	if endTimeStr == "" {
		log.Printf("GetCompanyRelatedDeviceInfoCount Error: endTime is required")
		c.JSON(http.StatusBadRequest, gin.H{"error": "End time is required"})
		return
	}

	// Convert string to Unix timestamp
	startUnix, _ := strconv.ParseInt(startTimeStr, 10, 64)
	endUnix, _ := strconv.ParseInt(endTimeStr, 10, 64)

	// Convert Unix timestamps to time.Time
	startTime := time.Unix(startUnix, 0)
	endTime := time.Unix(endUnix, 0)

	// Convert string to uint for CompanyID
	companyID, err := strconv.ParseUint(companyIDStr, 10, 32)
	if err != nil {
		log.Printf("GetCompanyRelatedDeviceInfoCount Error: Invalid Company ID: %v", err)
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Company ID"})
		return
	}

	// Get the counts
	inRangeCount, outOfRangeCount, monthlyCount, err := device.CountDeviceBreachedAndNormalDevicesByCompany(uint(companyID), startTime, endTime)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	data := models.NormalAndBreachedDevicesResponse{Breached: outOfRangeCount, Normal: inRangeCount, MonthlyCounts: monthlyCount}

	// Respond with both counts
	c.JSON(http.StatusOK, gin.H{"data": data})
}

func GetContractsMatchingDeviceLocation(c *gin.Context) {
	startTimeStr := c.DefaultQuery("start_time", "")
	endTimeStr := c.DefaultQuery("end_time", "")
	companyIDStr := c.DefaultQuery("company_id", "")

	if companyIDStr == "" {
		log.Printf("GetCompanyRelatedDeviceInfoCount Error: Company ID is required")
		c.JSON(http.StatusBadRequest, gin.H{"error": "Company ID is required"})
		return
	}

	// Convert string to uint for CompanyID
	companyID, err := strconv.ParseUint(companyIDStr, 10, 32)
	if err != nil {
		log.Printf("GetCompanyRelatedDeviceInfoCount Error: Invalid Company ID: %v", err)
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Company ID"})
		return
	}

	// Convert string to Unix timestamp
	startUnix, _ := strconv.ParseInt(startTimeStr, 10, 64)
	endUnix, _ := strconv.ParseInt(endTimeStr, 10, 64)

	// Convert Unix timestamps to time.Time
	startTime := time.Unix(startUnix, 0)
	endTime := time.Unix(endUnix, 0)

	matches, err := device.FetchMatchingContractsAndDeviceInfo(companyID, startTime, endTime)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": "Internal Server Error",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"data": len(matches),
	})
}
