package schema

import (
	"time"

	"gorm.io/gorm"
)

type ResourcePool struct {
	ID        uint64    `gorm:"primaryKey;not null;index:idx_rp_list_cursor,priority:6"`
	CreatedAt time.Time `gorm:"index:idx_rp_list_cursor,priority:5"`
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index;index:idx_rp_owner_live_name,priority:3;index:idx_rp_owner_live_cidr_start,priority:3;index:idx_rp_list_cursor,priority:4"`

	Name           string `gorm:"type:VARCHAR(255);not null;index"`
	NormalizedName string `gorm:"type:VARCHAR(255);not null;default:'';index:idx_rp_owner_live_name,priority:4"`

	OwnerType int    `gorm:"not null;index:idx_rp_owner_live_name,priority:1;index:idx_rp_owner_live_cidr_start,priority:1;index:idx_rp_list_cursor,priority:1"`
	OwnerId   uint64 `gorm:"not null;index;index:idx_rp_owner_live_name,priority:2;index:idx_rp_owner_live_cidr_start,priority:2;index:idx_rp_list_cursor,priority:2"`
	Cidr      string `gorm:"type:VARCHAR(255)"`
	// CidrStartIPv4 and CidrEndIPv4 are nullable until the compatibility
	// backfill canonicalizes existing resource pools.
	CidrStartIPv4  []byte `gorm:"type:BINARY(4);index:idx_rp_owner_live_cidr_start,priority:4"`
	CidrEndIPv4    []byte `gorm:"type:BINARY(4)"`
	ServicePlan    string `gorm:"type:VARCHAR(255);default:'';index:idx_rp_list_cursor,priority:3"`
	InstanceLimit  uint64 `gorm:"not null;default:50"`
	VcpusLimit     uint64 `gorm:"default:0"`
	K8sServiceCidr string `gorm:"type:VARCHAR(255)"`

	LifecycleState    string            `gorm:"type:VARCHAR(64);not null;default:''"`
	StateMessageCode  string            `gorm:"type:VARCHAR(255);not null;default:''"`
	VcpuLimitMode     string            `gorm:"type:VARCHAR(64);not null;default:'INHERITED'"`
	CloudResourceTags map[string]string `gorm:"column:cloud_resource_tags_json;serializer:json;type:JSON"`
	CreatedBy         *string           `gorm:"type:VARCHAR(255)"`
	PausedAt          *time.Time
	AutomaticResumeAt *time.Time

	ProviderId uint64 `gorm:"not null"`
	// Provider   *Provider `gorm:"foreignkey:ProviderId"`
	RegionId uint64 `gorm:"not null"`
	// Region   *Region `gorm:"foreignkey:RegionId"`
	ProviderIdentityId    uint64 `gorm:"not null;default:0"`
	DataplaneId           string `gorm:"not null"`
	PcaArn                string `gorm:"type:VARCHAR(2048);default:''"`
	PlacementGroupEnabled bool   `gorm:"not null;default:false"`
	HighAvailabilityType  string `gorm:"type:VARCHAR(255);default:''"`

	Provider *Provider `gorm:"foreignkey:ProviderId"`
	Region   *Region   `gorm:"foreignkey:RegionId"`
}

type ResourcePoolNamespace struct {
	OrganizationId uint64    `gorm:"type:BIGINT UNSIGNED;primaryKey;not null"`
	CreatedAt      time.Time `gorm:"type:DATETIME(3);not null"`
	UpdatedAt      time.Time `gorm:"type:DATETIME(3);not null"`
}

type ResourcePoolStrategy struct {
	gorm.Model

	OrganizationId        uint64 `gorm:"not null"`
	DefaultResourcePoolId uint64 `gorm:"not null"`
	ServicePlan           string `gorm:"type:VARCHAR(255);not null;default:''"`

	ProviderId uint64 `gorm:"not null"`
	// Provider   *Provider `gorm:"foreignkey:ProviderId"`
	RegionId uint64 `gorm:"not null"`
	// Region   *Region `gorm:"foreignkey:RegionId"`

	Provider *Provider `gorm:"foreignkey:ProviderId"`
	Region   *Region   `gorm:"foreignkey:RegionId"`
}
