package dto

import (
	"time"

	entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
	userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)

// === DTO Structs ===

type DashboardListDTO struct {
	Id          uint                     `json:"id"`
	Name        string                   `json:"name"`
	CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
	CreatedAt   time.Time                `json:"created_at"`
	UpdatedAt   time.Time                `json:"updated_at"`
}

type DashboardDetailDTO struct {
	DashboardListDTO
}

type DashboardFiltersDTO struct {
	StartDate      string   `json:"start_date"`
	EndDate        string   `json:"end_date"`
	AnalysisMode   string   `json:"analysis_mode"`
	ComparisonType string   `json:"comparison_type,omitempty"`
	Metric         string   `json:"metric,omitempty"`
	LokasiIds      []uint   `json:"location_ids"`
	FlockIds       []uint   `json:"flock_ids"`
	KandangIds     []uint   `json:"kandang_ids"`
	Include        []string `json:"include,omitempty"`
}

type DashboardStatisticsDTO struct {
	Label            string  `json:"label"`
	Value            float64 `json:"value"`
	PercentLastMonth float64 `json:"percent_last_month"`
}

type DashboardPerformanceOverviewDTO struct {
	StatisticsData []DashboardStatisticsDTO     `json:"statistics_data"`
	Charts         map[string]DashboardChartDTO `json:"charts,omitempty"`
}

type DashboardChartSeriesDTO struct {
	Id    string `json:"id"`
	Label string `json:"label"`
	Unit  string `json:"unit,omitempty"`
}

type DashboardChartDTO struct {
	Series  []DashboardChartSeriesDTO `json:"series"`
	Dataset []map[string]interface{}  `json:"dataset"`
}

// === Mapper Functions ===

func ToDashboardListDTO(e entity.Dashboard) DashboardListDTO {
	var createdUser *userDTO.UserRelationDTO
	if e.CreatedUser.Id != 0 {
		mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
		createdUser = &mapped
	}

	return DashboardListDTO{
		Id:          e.Id,
		Name:        e.Name,
		CreatedAt:   e.CreatedAt,
		UpdatedAt:   e.UpdatedAt,
		CreatedUser: createdUser,
	}
}

func ToDashboardListDTOs(e []entity.Dashboard) []DashboardListDTO {
	result := make([]DashboardListDTO, len(e))
	for i, r := range e {
		result[i] = ToDashboardListDTO(r)
	}
	return result
}
