package iam

import (
	"time"

	dbmodel "github.com/tidbcloud/aisupport/packages/sdk-go/infra/database/models"
)

type Role struct {
	ID               uint64           `gorm:"primary_key;not null"`
	CreatedAt        time.Time        `gorm:"created_at;type:TIMESTAMP;not null;<-:create"`
	UpdatedAt        *time.Time       `gorm:"updated_at;type:TIMESTAMP"`
	Name             string           `gorm:"type:VARCHAR(255);not null;uniqueIndex:idx_name"`
	Description      string           `gorm:"type:VARCHAR(255)"`
	AllowedTenantIDs dbmodel.UintList `gorm:"type:JSON"`
}

func (Role) TableName() string { return "iam_roles" }

type Policy struct {
	ID          uint64        `gorm:"primary_key;not null"`
	CreatedAt   time.Time     `gorm:"created_at;type:TIMESTAMP;not null;<-:create"`
	UpdatedAt   *time.Time    `gorm:"updated_at;type:TIMESTAMP"`
	Name        string        `gorm:"type:VARCHAR(255);not null;uniqueIndex:idx_name"`
	Type        string        `gorm:"type:VARCHAR(255);not null"`
	Description string        `gorm:"type:VARCHAR(255)"`
	Permissions []*Permission `gorm:"foreignKey:PolicyName;references:Name"`
}

func (Policy) TableName() string { return "iam_policies" }

type Permission struct {
	ID          uint64            `gorm:"primary_key;not null"`
	Resource    string            `gorm:"type:VARCHAR(255);not null"`
	Operations  dbmodel.StringSet `gorm:"type:set('*','get','post','put','delete','patch')"`
	Description string            `gorm:"type:VARCHAR(1024)"`
	PolicyName  string            `gorm:"type:VARCHAR(255);index:idx_policy_name;not null"`
}

func (Permission) TableName() string { return "iam_permissions" }

type RoleBinding struct {
	ID         uint64    `gorm:"primary_key;not null"`
	CreatedAt  time.Time `gorm:"created_at;type:TIMESTAMP;not null;<-:create"`
	TargetType string    `gorm:"type:VARCHAR(255);not null;index:idx_target;uniqueIndex:idx_binding"`
	TargetName string    `gorm:"type:VARCHAR(255);not null;index:idx_target;uniqueIndex:idx_binding"`
	RoleName   string    `gorm:"type:VARCHAR(255);not null;index:idx_role;uniqueIndex:idx_binding"`
}

func (RoleBinding) TableName() string { return "iam_role_bindings" }

type PolicyBinding struct {
	ID         uint64    `gorm:"primary_key;not null"`
	CreatedAt  time.Time `gorm:"created_at;type:TIMESTAMP;not null;<-:create"`
	PolicyName string    `gorm:"type:VARCHAR(255);not null;index:idx_policy;uniqueIndex:idx_binding"`
	RoleName   string    `gorm:"type:VARCHAR(255);not null;index:idx_role;uniqueIndex:idx_binding"`
}

func (PolicyBinding) TableName() string { return "iam_policy_bindings" }

type APIKey struct {
	ID        uint64    `gorm:"primary_key;not null"`
	CreatedAt time.Time `gorm:"created_at;type:TIMESTAMP;not null;<-:create"`
	Name      string    `gorm:"type:VARCHAR(255);not null;uniqueIndex:idx_name"`
	Comment   string    `gorm:"type:VARCHAR(255);not null"`
	Hash      string    `gorm:"type:VARCHAR(255);not null"`
	Owner     string    `gorm:"type:VARCHAR(255)"`
}

func (APIKey) TableName() string { return "iam_api_keys" }
