package model

import (
	"database/sql/driver"
	"encoding/json"
	"time"
)

// DetailsData represents the structure for details_json.
type DetailsData struct {
	CriticalDetails map[string]int `json:"critical_details,omitempty"`
	MajorDetails    map[string]int `json:"major_details,omitempty"`
}

// Value implements the driver.Valuer interface.
func (d DetailsData) Value() (driver.Value, error) {
	return json.Marshal(d)
}

// Scan implements the sql.Scanner interface.
func (d *DetailsData) Scan(value interface{}) error {
	return json.Unmarshal(value.([]byte), d)
}

// DailySummary corresponds to the daily_summary table.
type DailySummary struct {
	ID               int         `gorm:"primaryKey;autoIncrement"`
	ReportDate       string      `gorm:"type:date;not null"`
	OrgID            string      `gorm:"type:varchar(255);not null"`
	TotalSlowQueries int         `gorm:"not null"`
	UniqueDigests    int         `gorm:"not null"`
	CriticalCount    int         `gorm:"default:0"`
	MajorCount       int         `gorm:"default:0"`
	DetailsJson      DetailsData `gorm:"type:json"`
	CreatedAt        time.Time   `gorm:"type:datetime(3)"`
}

func (DailySummary) TableName() string {
	return "daily_summary"
}
