package models

import (
	// nolint:gosec
	"crypto/md5"
	"encoding/hex"
	"strconv"
	"strings"

	"github.com/tidbcloud/observability/o11ymetering/pkg/common/constants"
	"gorm.io/gorm"
)

type MonthlyStatisticReport struct {
	ID string `gorm:"primaryKey;size:32"`

	Timestamp int32  `gorm:"size:32;comment:''"`
	Vendor    string `gorm:"size:32;comment:''"`
	Region    string `gorm:"not null;size:32"`

	Env           constants.BizEnv        `gorm:"size:16;comment:''"`
	MeteringGroup constants.MeteringGroup `gorm:"not null;size:32;comment:''"`
	Tenant        string                  `gorm:"size:32;comment:''"`

	Category     constants.Category     `gorm:"size:16;comment:''"`
	SubCategory  string                 `gorm:"size:64;comment:''"`
	CloudService constants.CloudService `gorm:"size:32;comment:''"`

	Cost float64 `gorm:"size:64;comment:''"`
}

func (t *MonthlyStatisticReport) BeforeCreate(tx *gorm.DB) (err error) {
	if len(t.ID) == 0 {
		t.BuildID()
	}
	return nil
}

func (t *MonthlyStatisticReport) BuildID() string {
	strs := []string{
		strconv.Itoa(int(t.Timestamp)),
		t.Vendor,
		t.Region,
		string(t.Category),
		t.SubCategory,
		string(t.CloudService),
		string(t.Env),
		string(t.MeteringGroup),
		t.Tenant,
	}
	text := strings.Join(strs, "-")
	// nolint:gosec
	hash := md5.Sum([]byte(text))
	t.ID = hex.EncodeToString(hash[:])
	return t.ID
}

type O11yCost struct {
	Provider          string  `gorm:""`
	Region            string  `gorm:""`
	CloudService      string  `gorm:""`
	InnerCloudService string  `gorm:""`
	FixedCost         int     `gorm:""`
	Timestamp         int32   `gorm:""`
	Cost              float64 `gorm:""`
}

//
//type MonthlyCategoryCostProportion struct {
//	ID string `gorm:"primaryKey;size:32"`
//
//	Timestamp int32  `gorm:"size:32;comment:''"`
//	Region    string `gorm:"not null;size:32"`
//
//	Category     constants.Category `gorm:"size:16;comment:''"`
//	CloudService string             `gorm:"size:32;comment:''"`
//
//	ProportionInAllCategory float64 `gorm:"size:64;comment:''"`
//}
//
//func (t *MonthlyCategoryCostProportion) BeforeCreate(tx *gorm.DB) (err error) {
//	if len(t.ID) == 0 {
//		t.BuildID()
//	}
//	return nil
//}
//
//func (t *MonthlyCategoryCostProportion) BuildID() string {
//	strs := []string{
//		strconv.Itoa(int(t.Timestamp)),
//		t.Region,
//		string(t.Category),
//		t.CloudService,
//	}
//	text := strings.Join(strs, "-")
//	hash := md5.Sum([]byte(text))
//	t.ID = hex.EncodeToString(hash[:])
//	return t.ID
//}

type MonthlyCategoryStatistic struct {
	Region        string                  `gorm:"not null;size:32"`
	StatisticName constants.StatisticName `gorm:"not null;size:32;comment:'name of statistic'"`
	Category      constants.Category      `gorm:"size:16;comment:''"`
	SumValue      float64                 `gorm:"size:64;comment:''"`
}

type MonthlyTenantStatistic struct {
	Region        string                  `gorm:"not null;size:32"`
	StatisticName constants.StatisticName `gorm:"not null;size:32;comment:'name of statistic'"`
	Category      constants.Category      `gorm:"size:16;comment:''"`
	SubCategory   string                  `gorm:"size:64;comment:''"`

	MeteringGroup constants.MeteringGroup `gorm:"not null;size:32;comment:''"`
	Env           constants.BizEnv        `gorm:"size:16;comment:''"`
	Tenant        string                  `gorm:"size:32;comment:''"`

	SumValue float64 `gorm:"size:64;comment:''"`
}
