package entities

import (
	"time"

	"gorm.io/gorm"
)

const (
	IntegrationAPIKeyStatusActive  = "active"
	IntegrationAPIKeyStatusRevoked = "revoked"
)

type IntegrationAPIKey struct {
	ID              uint     `gorm:"primaryKey"`
	Name            string   `gorm:"type:varchar(100);not null"`
	Environment     string   `gorm:"type:varchar(50);not null;uniqueIndex:idx_integration_api_keys_env_prefix,priority:1"`
	Status          string   `gorm:"type:varchar(20);not null;default:active;index"`
	KeyPrefix       string   `gorm:"type:varchar(64);not null;uniqueIndex:idx_integration_api_keys_env_prefix,priority:2"`
	KeyHash         string   `gorm:"type:text;not null"`
	PermissionCodes []string `gorm:"type:jsonb;serializer:json;not null"`
	AllArea         bool     `gorm:"not null;default:false"`
	AreaIDs         []uint   `gorm:"type:jsonb;serializer:json;not null"`
	AllLocation     bool     `gorm:"not null;default:false"`
	LocationIDs     []uint   `gorm:"type:jsonb;serializer:json;not null"`
	LastUsedAt      *time.Time
	LastUsedFrom    string `gorm:"type:varchar(128)"`
	RevokedAt       *time.Time
	CreatedAt       time.Time      `gorm:"autoCreateTime"`
	UpdatedAt       time.Time      `gorm:"autoUpdateTime"`
	DeletedAt       gorm.DeletedAt `gorm:"index" json:"-"`
}

func (IntegrationAPIKey) TableName() string {
	return "integration_api_keys"
}
