package service

import "encoding/json"

type Intelligence struct {
	Source     string      `json:"source"`
	Confidence int         `json:"confidence"`
	Expired    bool        `json:"expired"`
	IntelTags  []TagsClass `json:"intel_tags"`
	FindTime   string      `json:"find_time"`
	IntelTypes []string    `json:"intel_types"`
	UpdateTime string      `json:"update_time"`
}

type OpenSource struct {
	Source     string      `json:"source"`
	Confidence int         `json:"confidence"`
	Expired    bool        `json:"expired"`
	IntelTags  []TagsClass `json:"intel_tags"`
	FindTime   string      `json:"find_time"`
	IntelTypes []string    `json:"intel_types"`
	UpdateTime string      `json:"update_time"`
}

type Location struct {
	Country     string `json:"country"`
	Province    string `json:"province"`
	City        string `json:"city"`
	Longitude   string `json:"lng"`
	Latitude    string `json:"lat"`
	CountryCode string `json:"country_code"`
}

type Basic struct {
	Carrier  string   `json:"carrier"`
	Location Location `json:"location"`
}

type ASN struct {
	Rank   int    `json:"rank"`
	Info   string `json:"info"`
	Number int    `json:"number"`
}

type Port struct {
	Port    int    `json:"port"`
	Module  string `json:"module"`
	Product string `json:"product"`
	Version string `json:"version"`
	Detail  string `json:"detail"`
}

type RDNS struct {
	RDNS    string `json:"rdns"`
	GetTime string `json:"get_time"`
}

type Intelligences struct {
	ThreatbookLab []Intelligence `json:"threatbook_lab"`
	XReward       []interface{}  `json:"x_reward"`
	OpenSource    []OpenSource   `json:"open_source"`
}

type Sample struct {
	Hash          string `json:"sha256"`
	ScanTime      string `json:"scan_time"`
	Ratio         string `json:"ratio"`
	MalwareType   string `json:"malware_type"`
	MalwareFamily string `json:"malware_family"`
}

type TagsClass struct {
	TagsType string   `json:"tags_type"` //标签类别，如"industry(行业)"、"gangs（团伙）"、"virus_family（家族）"等
	Tags     []string `json:"tags"`      //具体的攻击团伙或安全事件标签，例如：APT、海莲花等。
}

type CAS struct {
	Protocol           string      `json:"protocol"`
	Port               int16       `json:"port"`
	Period             int         `json:"period"`
	DigitalCertificate Certificate `json:"digital_certificate"`
}

type Certificate struct {
	Subject      string `json:"subject"`
	Issuer       string `json:"issuer"`
	Fingerprint  string `json:"fingerprint"`
	Purpose      string `json:"purpose"`
	Verify       string `json:"verify"`
	Status       string `json:"status"`
	Revoked      bool   `json:"revoked"`
	Begin        string `json:"begin"`
	End          string `json:"end"`
	StatusDesc   string `json:"status_desc"`
	SerialNumber string `json:"serial_number"`
	RevokedTime  string `json:"revoked_time"`
}

type IpInfo struct {
	Samples       []Sample      `json:"samples"`
	TagsClasses   []TagsClass   `json:"tags_classes"`
	Judgments     []string      `json:"judgments"`
	Intelligences Intelligences `json:"intelligences"`
	Scene         string        `json:"scene"`
	Basic         Basic         `json:"basic"`
	ASN           ASN           `json:"asn"`
	Ports         []Port        `json:"ports"`
	CAS           []CAS         `json:"cas"`
	UpdateTime    string        `json:"update_time"`
	RDNSList      []RDNS        `json:"rdns_list"`
	SumCurDomains string        `json:"sum_cur_domains"`
}

type RespData struct {
	Data         map[string]json.RawMessage `json:"data"`
	ResponseCode int                        `json:"response_code"`
	VerboseMsg   string                     `json:"verbose_msg"`
}

type Reputation struct {
	IsMalicious bool `json:"is_malicious"`
}

type ASNPrivate struct {
	Rank   int    `json:"rank"`
	Info   string `json:"info"`
	Number string `json:"number"`
}

type LocationPrivate struct {
	Country     string  `json:"country"`
	Province    string  `json:"province"`
	City        string  `json:"city"`
	Longitude   float64 `json:"lng"`
	Latitude    float64 `json:"lat"`
	CountryCode string  `json:"country_code"`
}

type BasicPrivate struct {
	Carrier  string          `json:"carrier"`
	Location LocationPrivate `json:"location"`
}

type TagsClassPrivate map[string][]string

type IPInfoPrivate struct {
	Judgments   []string         `json:"judgments"`
	Basic       BasicPrivate     `json:"basic"`
	ASN         ASNPrivate       `json:"asn"`
	Scene       string           `json:"scene"`
	IsMalicious bool             `json:"is_malicious"`
	TagsClasses TagsClassPrivate `json:"tags_classes"`
}

type IPInfoPrivateRespData struct {
	IOC          string          `json:"ioc"`
	Intelligence []IPInfoPrivate `json:"intelligence"`
}

type IPInfoPrivateResp struct {
	Data         []IPInfoPrivateRespData `json:"data"`
	ResponseCode int                     `json:"response_code"`
	VerboseMsg   string                  `json:"verbose_msg"`
}

type WafService struct {
	ID          uint32   `json:"id"`
	GatewayName string   `json:"gateway_name"`
	Name        string   `json:"name"`
	Mode        string   `json:"mode"`
	RuleNum     int      `json:"rule_num"`
	AttackNum   int      `json:"attack_num"`
	Listeners   []string `json:"listeners"`
} // WAF configuration details

type GatewateInfo struct {
	GatewayName   string `json:"gateway_name"`
	Namespace     string `json:"namespace"`
	RegionCode    string `json:"region_code"`
	ApiGatewayCrn string `json:"gateway_crn"`
	// Hosts         []string `json:"hosts"`
}

type GetWafGatewayInfoReq struct {
	GatewateInfo
	Cookie string `json:"cookie"`
}

type CreateWafReq struct {
	GatewateInfo
	Port         uint32   `json:"port"`
	Host         []string `json:"host"`
	Mode         WafMode  `json:"mode"`
	ListenerName string   `json:"listener_name"`
	ServiceID    uint32   `json:"service_id"`
}

type DeleteWafReq struct {
	GatewateInfo
	Ports []int `json:"ports"`
}

type RuleRequest struct {
	GatewateInfo
	CategoryID []string `json:"category_id"`
	Status     int      `json:"status"`
}

type WafMode string

const (
	WafModeProtect     WafMode = "protect"
	WafModeAlert       WafMode = "alert"
	WafModePassthrough WafMode = "passthrough"
)

type UpdateModeReq struct {
	Mode        WafMode `json:"mode"`
	GatewayName string  `json:"gateway_name"`
	Namespace   string  `json:"namespace"`
	RegionCode  string  `json:"region_code"`
}

type EnableListenerWafReq struct {
	GatewateInfo
	Enable       bool     `json:"enable"`
	Hosts        []string `json:"hosts"`
	Port         int      `json:"port"`
	Mode         WafMode  `json:"mode"`
	ListenerName string   `json:"listener_name"`
}

type EnableGatewayWafReq struct {
	GatewateInfo
	Enable bool   `json:"enable"`
	Cookie string `json:"cookie"`
}

type ListenerWaf struct {
	Hosts []string `json:"hosts"`
	// Port         int      `json:"port"`
	HostsAndPort string `json:"hosts_and_port"`
	Name         string `json:"name"`
}

type EnableListenerWafsReq struct {
	GatewateInfo
	Enable    bool          `json:"enable"`
	Listeners []ListenerWaf `json:"listeners"`
}

type WafRule struct {
	ID          int    `json:"id"`
	Level       int    `json:"level"`
	Name        string `json:"name"`
	Type        string `json:"type"`
	Description string `json:"description"`
	Expr        string `json:"expr"`
	Mode        string `json:"mode"`
}

type Catagory struct {
	EN string `json:"en"`
	Zh string `json:"zh"`
}

type Description struct {
	EN string `json:"en"`
	Zh string `json:"zh"`
}

type WafRuleCategory struct {
	CategoryID  string      `json:"category_id"`
	Status      int         `json:"status,omitempty"`
	Category    Catagory    `json:"category"`
	Description Description `json:"description"`
	Rules       []WafRule   `json:"rules"`
}

type RuleGroupResp struct {
	CategoryID  string `json:"category_id"`
	Category    string `json:"category"`
	Description string `json:"description"`
	Status      int    `json:"status"`
}

type GatewayListener struct {
	GatewayName string   `json:"gateway_name"`
	Namespace   string   `json:"namespace"`
	RegionCode  string   `json:"region_code"`
	Hosts       []string `json:"hosts"`
	Port        int      `json:"port"`
	Enable      bool     `json:"enable"`
}

type CreateListenerReq struct {
	GatewateInfo
	Port int `json:"port"`
}

type DeleteListenerReq struct {
	GatewateInfo
	Port         int    `json:"port"`
	ListenerName string `json:"listener_name"`
}

type GatewayRespListenerData struct {
	GatewayName       string   `json:"gateway_name"`
	Namespace         string   `json:"namespace"`
	ListenerName      string   `json:"listener_name"`
	ApiGatewayCrn     string   `json:"apigateway_crn"`
	CreateAccountName string   `json:"create_account_name"`
	CreateAccountID   string   `json:"create_account_id"`
	Hosts             []string `json:"hosts"`
	Port              int      `json:"port"`
}

type GatewayResponseBase struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

type GatewayListenerResponse struct {
	GatewayResponseBase
	Data GatewayRespListenerData `json:"data"`
}

type GatewayListenerResponseList struct {
	GatewayResponseBase
	Data []GatewayRespListenerData `json:"data"`
}

type AttackLog struct {
	Uuid           string `json:"uuid"`
	AttackIp       string `json:"attack_ip"`
	AttackedAddr   string `json:"attacked_addr"`
	AttackType     string `json:"attack_type"`
	AttackTime     int64  `json:"attack_time"`
	AttackedApp    string `json:"attacked_app"`
	AttackListener string `json:"attack_listener"`
	AttackLoad     string `json:"attack_load"`
	ClusterKey     string `json:"cluster_key"`
	Action         string `json:"action"`
	RuleName       string `json:"rule_name"`
	RequestPkg     string `json:"req_pkg"`
}

type AttackRsp struct {
	Uuid        string `json:"uuid"`
	Intact      bool   `json:"intact"`
	ContentType string `json:"content_type"`
	RspPkg      string `json:"rspPkg"`
}
type AttackLogFilter struct {
	Offset         int    `json:"offset"`
	Limit          int    `json:"limit"`
	ServiceId      int64  `json:"service_id"`
	Cluster        string `json:"cluster"`
	AttackUrl      string `json:"attack_url"`
	AttackIp       string `json:"attack_ip"`
	AttackType     string `json:"attack_type"`
	AttackApp      string `json:"attack_app"` // gateway name
	AttackListener string `json:"attack_listener"`
	Action         string `json:"action"`
	Token          string `json:"token"`
	StartTime      int64  `json:"start_time"`
	EndTime        int64  `json:"end_time"`
}

type MatcherExpr struct {
	ID        uint32   `json:"id"`
	Name      string   `json:"name,omitempty"`
	Scope     []uint32 `json:"scope,omitempty"`
	ScopeName []string `json:"scope_name,omitempty"`
	Mode      string   `json:"mode,omitempty"`
	Expr      string   `json:"expr,omitempty"`
	Global    bool     `json:"global"`
	Status    int32    `json:"status"`
}

type MatchExprQueryOption struct {
	WhereLikeCondition map[string]string
	whereEqCondition   map[string]interface{}
	whereInCondition   map[string]interface{}
}

func MatchExprQuery() *MatchExprQueryOption {
	return &MatchExprQueryOption{
		WhereLikeCondition: make(map[string]string, 3),
		whereEqCondition:   make(map[string]interface{}, 3),
		whereInCondition:   make(map[string]interface{}, 3),
	}
}

func (n *MatchExprQueryOption) WithFuzzyName(name string) *MatchExprQueryOption {
	n.WhereLikeCondition["name"] = name
	return n
}

func (n *MatchExprQueryOption) WithFuzzyExpr(expr string) *MatchExprQueryOption {
	n.WhereLikeCondition["expr"] = expr
	return n
}

func (n *MatchExprQueryOption) WithStatus(status int32) *MatchExprQueryOption {
	n.whereEqCondition["status"] = status
	return n
}

func (n *MatchExprQueryOption) WithMode(mode string) *MatchExprQueryOption {
	n.whereEqCondition["mode"] = mode
	return n
}

func (q *MatchExprQueryOption) WithInConditionCustom(column string, value interface{}) *MatchExprQueryOption {
	q.whereInCondition[column] = value
	return q
}

type WafListenerHistory struct {
	Name         string `json:"name"`
	GatewayName  string `json:"gateway_name"`
	ListenerName string `json:"listener_name"`
	Namespace    string `json:"namespace"`
	RegionCode   string `json:"region_code"`
	Description  string `json:"description"`
	Status       int    `json:"status"`
	Operation    string `json:"operation"`
}
type WafListenerHistoryOption struct {
	WhereLikeCondition map[string]string
	WhereEqCondition   map[string]interface{}
	WhereInCondition   map[string]interface{}
}

func WafListenerHistoryQuery() *WafListenerHistoryOption {
	return &WafListenerHistoryOption{
		WhereLikeCondition: make(map[string]string, 3),
		WhereEqCondition:   make(map[string]interface{}, 3),
		WhereInCondition:   make(map[string]interface{}, 3),
	}
}

func (n *WafListenerHistoryOption) WithFuzzyName(name string) *WafListenerHistoryOption {
	n.WhereLikeCondition["name"] = name
	return n
}

func (n *WafListenerHistoryOption) WithFuzzyListenerName(listenerName string) *WafListenerHistoryOption {
	n.WhereLikeCondition["listener_name"] = listenerName
	return n
}

func (n *WafListenerHistoryOption) WithFuzzyGatewayName(gatewayName string) *WafListenerHistoryOption {
	n.WhereLikeCondition["gateway_name"] = gatewayName
	return n
}

func (n *WafListenerHistoryOption) WithStatus(status int32) *WafListenerHistoryOption {
	n.WhereEqCondition["status"] = status
	return n
}

func (n *WafListenerHistoryOption) WithOperation(operation string) *WafListenerHistoryOption {
	n.WhereEqCondition["operation"] = operation
	return n
}

func (n *WafListenerHistoryOption) WithFuzzyRegionCode(regionCode string) *WafListenerHistoryOption {
	n.WhereLikeCondition["region_code"] = regionCode
	return n
}

type AttackClasses struct {
	Id         int    `json:"id"`
	En         string `json:"en,omitempty"`
	Describe   string `json:"describe"`
	AttackType string `json:"type"`
}
