package schema

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

	"gorm.io/gorm"
)

type PublicConnectionSetting struct {
	gorm.Model

	ClusterId   uint64 `gorm:"not null;index"` // Cluster ID
	TidbGroupId uint64 `gorm:"not null;index"` // TiDB Group ID

	DisablePublicLB bool           `gorm:"default:false"`
	Cidr            TrafficFilters `gorm:"type:JSON"`
}

type TrafficFilter struct {
	Cidr        string `json:"cidr"`
	Description string `json:"description"`
}

type TrafficFilters struct {
	Cidrs []TrafficFilter `json:"cidrs"`
}

// Value marshal json
func (c TrafficFilters) Value() (driver.Value, error) {
	b, err := json.Marshal(c)
	return string(b), err
}

// Scan unmarshal json
func (c *TrafficFilters) Scan(input any) error {
	return json.Unmarshal(input.([]byte), c)
}
