package controllers

import (
	"math/rand"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

type EnergyDataPoint struct {
	Time  string  `json:"time"`
	Value float64 `json:"value"`
	Type  string  `json:"type"` // "Lighting", "AC", "Power"
}

func GetEnergyAnalysis(c *gin.Context) {
	// Mock Data Generator for Charts
	// In real app: Query TDengine
	
	period := c.Query("period") // "day", "month"
	
	var data []EnergyDataPoint
	now := time.Now()
	
	if period == "month" {
		// Generate daily data for current month
		for i := 1; i <= 30; i++ {
			dateStr := now.AddDate(0, 0, -30+i).Format("2006-01-02")
			
			// Lighting
			data = append(data, EnergyDataPoint{
				Time:  dateStr,
				Value: 50 + rand.Float64()*20,
				Type:  "Lighting",
			})
			// AC
			data = append(data, EnergyDataPoint{
				Time:  dateStr,
				Value: 100 + rand.Float64()*50,
				Type:  "AC",
			})
		}
	} else {
		// Generate hourly data for today
		for i := 0; i < 24; i++ {
			timeStr := now.Truncate(24*time.Hour).Add(time.Duration(i)*time.Hour).Format("15:00")
			
			data = append(data, EnergyDataPoint{
				Time:  timeStr,
				Value: 10 + rand.Float64()*5,
				Type:  "Lighting",
			})
			data = append(data, EnergyDataPoint{
				Time:  timeStr,
				Value: 20 + rand.Float64()*10,
				Type:  "AC",
			})
		}
	}
	
	c.JSON(http.StatusOK, data)
}

