package activity

import "time"

const (
	AssetTypeMemory     = "memory"
	AssetTypeSkill      = "skill"
	AssetTypeKB         = "kb"
	AssetTypeRole       = "role"
	AssetTypeAutomation = "automation"
	EventTypeCommit     = "commit_pushed"

	SourceControl      = "control"
	SourceGitea        = "gitea"
	SourceAGS          = "ags"
	SourceContentStore = "content_store"
)

// ResourceActivityEvent is the persisted, append-only activity projection for
// resource-domain assets such as Memory, Skill, and Knowledge Base. It is not a
// AgentTeam server task/message activity model or an API response DTO. SummaryKey and
// DetailsJSON retain renderable facts rather than a localized sentence so the
// service layer can produce a presentation for the user's current language.
type ResourceActivityEvent struct {
	ID            string    `gorm:"primaryKey;size:64;index:idx_resource_activity_commit_lookup,priority:6"`
	WorkspaceID   string    `gorm:"size:64;not null;index:idx_resource_activity_scope,priority:1;index:idx_resource_activity_actor,priority:1;index:idx_resource_activity_workspace_time,priority:1;index:idx_resource_activity_commit_lookup,priority:1;uniqueIndex:uk_resource_activity_source_event,priority:1"`
	AssetType     string    `gorm:"size:32;not null;index:idx_resource_activity_scope,priority:2;index:idx_resource_activity_commit_lookup,priority:2;index"`
	AssetID       string    `gorm:"size:64;not null;index:idx_resource_activity_scope,priority:3;index:idx_resource_activity_commit_lookup,priority:4;index"`
	RepoFullName  string    `gorm:"size:255;not null;default:'';index"`
	EventType     string    `gorm:"size:128;not null;index:idx_resource_activity_commit_lookup,priority:3;index"`
	ActorType     string    `gorm:"size:32;not null;default:''"`
	ActorID       string    `gorm:"size:64;not null;default:'';index:idx_resource_activity_actor,priority:2"`
	ActorName     string    `gorm:"size:255;not null;default:''"`
	SummaryKey    string    `gorm:"size:255;not null;default:''"`
	DetailsJSON   string    `gorm:"column:details_json;type:text;not null"`
	TargetKind    string    `gorm:"size:64;not null;default:'';index"`
	TargetID      string    `gorm:"size:255;not null;default:''"`
	TargetURL     string    `gorm:"type:text;not null"`
	Source        string    `gorm:"size:32;not null;default:'control';uniqueIndex:uk_resource_activity_source_event,priority:2"`
	SourceEventID *string   `gorm:"size:255;uniqueIndex:uk_resource_activity_source_event,priority:3"`
	OccurredAt    time.Time `gorm:"not null;index:idx_resource_activity_scope,priority:4;index:idx_resource_activity_workspace_time,priority:2;index:idx_resource_activity_actor,priority:3;index:idx_resource_activity_commit_lookup,priority:5"`
	CreatedAt     time.Time `gorm:"not null"`
}

func (ResourceActivityEvent) TableName() string { return "resource_activity_events" }

// ResourceActivityLatestCommit is the persisted latest-commit projection for
// one resource asset. It supports resource list ordering without scanning the
// append-only ResourceActivityEvent history and is not a source-control commit
// record owned by AgentTeam Core or embedded AGS.
type ResourceActivityLatestCommit struct {
	WorkspaceID string    `gorm:"primaryKey;size:64"`
	AssetType   string    `gorm:"primaryKey;size:32"`
	AssetID     string    `gorm:"primaryKey;size:64"`
	OccurredAt  time.Time `gorm:"not null"`
	EventID     string    `gorm:"size:64;not null"`
}

func (ResourceActivityLatestCommit) TableName() string {
	return "resource_activity_latest_commits"
}

const (
	ProjectorKindCommits  = "commits"
	ProjectorKindIssues   = "issues"
	ProjectorKindPulls    = "pulls"
	ProjectorKindReviews  = "reviews"
	ProjectorKindTags     = "tags"
	ProjectorKindReleases = "releases"
	ProjectorKindWiki     = "wiki"

	ProjectorStatusOK          = "ok"
	ProjectorStatusError       = "error"
	ProjectorStatusRateLimited = "rate_limited"
)

// ResourceActivityProjectorCheckpoint is the persisted cursor and diagnostic
// state for one resource repository event stream. It is local to the resource
// activity projector rather than a generic AgentTeam worker checkpoint. The
// projector intentionally writes all data-plane events without checking user
// grants; read services enforce permissions when listing activity.
type ResourceActivityProjectorCheckpoint struct {
	ID                string     `gorm:"primaryKey;size:64"`
	WorkspaceID       string     `gorm:"size:64;not null;uniqueIndex:uk_resource_activity_projector_checkpoint,priority:1;index:idx_resource_activity_projector_repo,priority:1"`
	ResourceType      string     `gorm:"size:64;not null;uniqueIndex:uk_resource_activity_projector_checkpoint,priority:2"`
	ResourceID        string     `gorm:"size:64;not null;uniqueIndex:uk_resource_activity_projector_checkpoint,priority:3"`
	AssetType         string     `gorm:"size:32;not null;index"`
	AssetID           string     `gorm:"size:64;not null;index"`
	RepoRole          string     `gorm:"size:64;not null;uniqueIndex:uk_resource_activity_projector_checkpoint,priority:4"`
	RepoFullName      string     `gorm:"size:255;not null;index:idx_resource_activity_projector_repo,priority:2"`
	EventKind         string     `gorm:"size:64;not null;uniqueIndex:uk_resource_activity_projector_checkpoint,priority:5"`
	LastSeenAt        *time.Time `gorm:"index"`
	LastSourceEventID string     `gorm:"size:255;not null;default:''"`
	StateJSON         string     `gorm:"column:state_json;type:text;not null"`
	Status            string     `gorm:"size:32;not null;default:'ok';index"`
	LastError         string     `gorm:"type:text;not null"`
	LastErrorAt       *time.Time `gorm:"index"`
	LastPolledAt      *time.Time `gorm:"index"`
	ConsecutiveErrors int        `gorm:"not null;default:0"`
	CreatedAt         time.Time  `gorm:"not null"`
	UpdatedAt         time.Time  `gorm:"not null;index"`
}

func (ResourceActivityProjectorCheckpoint) TableName() string {
	return "resource_activity_projector_checkpoints"
}
