package model

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

	"github.com/rs/zerolog/log"
)

type HostList []string

func (h HostList) Value() (driver.Value, error) {
	return json.Marshal(h)
}

func (h *HostList) Scan(src interface{}) error {
	log.Info().Msgf("Scan host list: %v", src)
	return json.Unmarshal(src.([]byte), h)
}

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

func (r *RuleCategoryStatus) Scan(src interface{}) error {
	return json.Unmarshal(src.([]byte), r)
}

func (r RuleCategoryStatus) Value() (driver.Value, error) {
	return json.Marshal(r)
}

type RuleCategoryStatusList []RuleCategoryStatus

func (r RuleCategoryStatusList) Value() (driver.Value, error) {
	return json.Marshal(r)
}

func (r *RuleCategoryStatusList) Scan(src interface{}) error {
	return json.Unmarshal(src.([]byte), r)
}

const (
	WafStatusEnable  = 0
	WafStatusDisable = 1
	WafStatusUnknown = 2
)

type WafService struct {
	ID                 uint32              `gorm:"column:id;primaryKey;autoIncrement"`
	GatewayName        string              `gorm:"column:gateway_name"`
	Namespace          string              `gorm:"column:namespace"`
	RegionCode         string              `gorm:"column:region_code"`
	Mode               string              `gorm:"column:mode"`
	RuleNum            int                 `gorm:"column:rule_num"`
	AttackNum          int                 `gorm:"column:attack_num"`
	RuleCategoryStatus *RuleCategoryStatus `gorm:"column:rule_category_status;type:json"`
	// ListenerWafs       string              `gorm:"column:listener_wafs"`
	// Host               HostList            `gorm:"column:host"`
}

func (WafService) TableName() string {
	return "waf_services"
}

type WafRule struct {
	ID         int    `json:"id"`
	CategoryID string `gorm:"column:category_id"`
	Level      int    `json:"level"`
	// Status      int    `gorm:"column:status"`
	Name        string `json:"name"`
	Type        string `json:"type"`
	Description string `json:"description"`
	Expr        string `json:"expr"`
	Mode        string `json:"mode"`
}

func (WafRule) TableName() string {
	return "waf_rules"
}

type RuleList []WafRule

func (r RuleList) Value() (driver.Value, error) {
	return json.Marshal(r)
}

func (r *RuleList) Scan(src interface{}) error {
	return json.Unmarshal(src.([]byte), r)
}

type WafRuleCategory struct {
	ID            int      `gorm:"column:id;primaryKey;autoIncrement"`
	CategoryID    string   `gorm:"column:category_id"`
	CategoryEN    string   `gorm:"column:category_en"`
	CategoryZH    string   `gorm:"column:category_zh"`
	DescriptionEN string   `gorm:"column:description_en"`
	DescriptionZH string   `gorm:"column:description_zh"`
	Status        int      `gorm:"column:status"`
	Rules         RuleList `gorm:"column:rules"`
}

func (WafRuleCategory) TableName() string {
	return "waf_rule_categories"
}

func (r *WafRuleCategory) Scan(src interface{}) error {
	err := json.Unmarshal(src.([]byte), r)
	if err != nil {
		return err
	}
	return nil
}

func (r WafRuleCategory) Value() (driver.Value, error) {
	return json.Marshal(r)
}

type GatewayListener struct {
	ID          int      `gorm:"column:id;primaryKey;autoIncrement"`
	GatewayName string   `gorm:"column:gateway_name"`
	Namespace   string   `gorm:"column:namespace"`
	RegionCode  string   `gorm:"column:region_code"`
	Port        int      `gorm:"column:port"`
	Enable      bool     `gorm:"column:enable"`
	Hosts       HostList `gorm:"column:hosts"`
}

func (GatewayListener) TableName() string {
	return "gateway_listeners"
}

type IntSlice []uint32

func (sl *IntSlice) Scan(value interface{}) error {
	b, ok := value.([]byte)
	if !ok {
		return errors.New("type assertion to []byte failed")
	}
	return json.Unmarshal(b, &sl)
}
func (sl IntSlice) Value() (driver.Value, error) {
	data, err := json.Marshal(sl)
	return data, err
}

type MatcherExpr struct {
	ID     uint32   `gorm:"column:id;primaryKey;autoIncrement"`
	Name   string   `gorm:"column:name"`
	Scope  IntSlice `gorm:"column:scope"`
	Mode   string   `gorm:"column:mode"`
	Expr   string   `gorm:"column:expr"`
	Global bool     `gorm:"column:global"`
	Status int      `gorm:"column:status"`
}

func (MatcherExpr) TableName() string {
	return "waf_blackwhitelists"
}

type Model struct {
	ID        uint      `gorm:"primarykey"`
	CreatedAt time.Time `gorm:"column:created_at"`
	UpdatedAt time.Time `gorm:"column:updated_at"`
}

type Status int

const (
	StatusSuccess Status = iota
	StatusFailure
)

type Operation string

const (
	OperationCreate Operation = "创建"
	OperationDelete Operation = "删除"
)

type WafListenerHistory struct {
	Model
	Name         string    `gorm:"column:name"`
	GatewayName  string    `gorm:"column:gateway_name"`
	ListenerName string    `gorm:"column:listener_name"`
	Namespace    string    `gorm:"column:namespace"`
	RegionCode   string    `gorm:"column:region_code"`
	Description  string    `gorm:"column:description"`
	Status       Status    `gorm:"column:status"`
	Operation    Operation `gorm:"column:operation"`
}

func (WafListenerHistory) TableName() string {
	return "waf_listener_histories"
}
