package caasmmodel

import (
	"time"

	coremodel "security-center-backend/internal/model"
)

const (
	APIRouteConfigSourceKongYAML = "kong_yaml"

	APIRouteConfigStatusActive   = "active"
	APIRouteConfigStatusInactive = "inactive"
	APIRouteConfigStatusInvalid  = "invalid"

	APIRouteKindTestCase = "test_case"
	APIRouteKindRegex    = "regex"
)

type APIRouteConfig struct {
	coremodel.Base
	Name         string     `gorm:"size:255;not null" json:"name"`
	SourceType   string     `gorm:"size:32;not null;default:kong_yaml" json:"source_type"`
	RawYAML      string     `gorm:"type:longtext;not null" json:"raw_yaml,omitempty"`
	YAMLSHA256   string     `gorm:"column:yaml_sha256;size:64;index" json:"yaml_sha256"`
	Status       string     `gorm:"size:32;not null;default:active" json:"status"`
	ErrorMessage string     `gorm:"type:text" json:"error_message,omitempty"`
	RouteCount   int        `gorm:"not null;default:0" json:"route_count"`
	UploadedBy   *uint64    `gorm:"index" json:"uploaded_by,omitempty"`
	IsActive     bool       `gorm:"index:idx_api_route_configs_active_deleted,priority:1" json:"is_active"`
	ActivatedAt  *time.Time `json:"activated_at,omitempty"`
}

func (APIRouteConfig) TableName() string {
	return "caasm_api_route_configs"
}

type APIRouteAsset struct {
	coremodel.Base
	ConfigID          uint64                `gorm:"not null;uniqueIndex:uk_api_route_config_route_key,priority:1;index:idx_api_route_assets_config_deleted,priority:1" json:"config_id"`
	Config            APIRouteConfig        `json:"-"`
	GroupIndex        int                   `gorm:"not null;default:0" json:"group_index"`
	IngressIndex      int                   `gorm:"not null;default:0" json:"ingress_index"`
	ServiceIndex      int                   `gorm:"not null;default:0" json:"service_index"`
	RouteIndex        int                   `gorm:"not null;default:0" json:"route_index"`
	RouteKind         string                `gorm:"size:32;not null;index" json:"route_kind"`
	RoutePath         string                `gorm:"size:2048;not null;index:idx_api_route_path,priority:1,length:255" json:"route_path"`
	RouteKey          string                `gorm:"size:64;not null;uniqueIndex:uk_api_route_config_route_key,priority:2" json:"route_key"`
	Domains           coremodel.RawJSON     `gorm:"type:json" json:"domains"`
	NamespaceMap      coremodel.RawJSON     `gorm:"type:json" json:"namespace_map"`
	IngressNamePrefix string                `gorm:"size:255;not null;default:''" json:"ingress_name_prefix"`
	ServiceName       string                `gorm:"size:255;not null;index" json:"service_name"`
	ServicePort       *int                  `json:"service_port,omitempty"`
	External          bool                  `gorm:"not null;default:false" json:"external"`
	StripPath         bool                  `gorm:"not null;default:false" json:"strip_path"`
	Plugins           coremodel.StringArray `gorm:"type:json" json:"plugins"`
	RawPlugins        string                `gorm:"type:text" json:"raw_plugins,omitempty"`
	OccurrenceCount   int                   `gorm:"not null;default:1" json:"occurrence_count"`
}

func (APIRouteAsset) TableName() string {
	return "caasm_api_route_assets"
}
