package schema

import "gorm.io/gorm"

type Region struct {
	gorm.Model

	Name              string `gorm:"index:idx_name_provider,unique"` // logical name, such as: ap-northeast-www-1
	RegionName        string `gorm:"index"`                          // region name, such as: ap-northeast-1
	ProviderId        uint64 `gorm:"index"`
	DisplayName       string
	CountryCode       string `gorm:"index"` // country code(https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). such as: CN, US.
	Area              string `gorm:"index"`
	Priority          int32  `gorm:"default:999999"`
	DefaultBackupTime string `gorm:"default:'00:00'"`
	ServiceAddress    string `gorm:"type:VARCHAR(255);not null"`
	// Deprecated: service plan is no longer sourced from region-level field.
	// Use RegionServicePlan.ServicePlan instead.
	ServicePlan string `gorm:"type:ENUM('Starter', 'Essential', 'Premium', 'BYOC');not null"`
	// Deprecated: region-level visibility is no longer used. Use RegionServicePlan.IsPublic instead.
	IsPublic        *bool  `gorm:"default:true"`
	Status          string `gorm:"index;type:ENUM('active', 'inactive')"`
	SharedBucketUrl string // shared bucket url of the region, if not set, keep empty

	Provider                 *Provider                 `gorm:"foreignkey:ProviderId"`
	RegionTenants            []*RegionTenant           // A region has no or many region tenants.
	RegionServicePlans       []*RegionServicePlan      // A region supports one or many service plans.
	RegionOpsAuditLogSetting *RegionOpsAuditLogSetting // A region has one or zero ops audit log setting.
}

type RegionServicePlan struct {
	gorm.Model

	RegionId    uint64 `gorm:"index;uniqueIndex:idx_region_service_plan,priority:1"`
	ServicePlan string `gorm:"type:ENUM('Starter', 'Essential', 'Premium', 'BYOC', 'Premium_Reserved', 'Essential_V2');not null;uniqueIndex:idx_region_service_plan,priority:2"`
	IsPublic    *bool  `gorm:"default:true"`
}

type RegionTenant struct {
	gorm.Model

	RegionId            uint64 `gorm:"index"`
	RegionServicePlanId uint64 `gorm:"index"`
	TenantId            uint64 `gorm:"index"`
}

type RegionOpsAuditLogSetting struct {
	gorm.Model

	Enabled               *bool  `gorm:"default:false"`
	Type                  string `gorm:"type:VARCHAR(64);not null"`
	BucketUri             string `gorm:"type:VARCHAR(255)"`
	RoleArn               string `gorm:"type:VARCHAR(255)"`
	AccountId             string `gorm:"type:VARCHAR(255)"`
	ExternalId            string `gorm:"type:VARCHAR(255)"`
	FileSizeLimitInMb     uint64 `gorm:"not null;default:0"`
	RotationIntervalInMin uint64 `gorm:"not null;default:0"`

	RegionId uint64 `gorm:"not null"`
}
