package repocache

import "time"

const (
	StatusPending   = "pending"
	StatusBuilding  = "building"
	StatusReady     = "ready"
	StatusFailed    = "failed"
	StatusCancelled = "cancelled"
)

// Cache is a persisted registry entry for a reusable public Git bare mirror.
// Status tracks the latest build attempt, while ActiveGeneration independently
// selects the immutable serving version. Revision linearizes settings and build
// transitions across service instances. Operators cannot direct Git writes outside
// the configured cache root.
type Cache struct {
	ID                  string     `gorm:"primaryKey;size:64"`
	NormalizedRemoteURL string     `gorm:"column:normalized_remote_url;size:768;not null;uniqueIndex"`
	DisplayRemoteURL    string     `gorm:"column:display_remote_url;size:1024;not null"`
	Status              string     `gorm:"size:32;not null;default:'pending';index"`
	ActiveGeneration    string     `gorm:"column:active_generation;size:64;not null;default:''"`
	AutoRefreshEnabled  bool       `gorm:"not null;default:false;index"`
	RefreshIntervalSecs int        `gorm:"column:refresh_interval_seconds;not null;default:86400"`
	LastSyncAt          *time.Time `gorm:"index"`
	LastSuccessAt       *time.Time `gorm:"index"`
	NextSyncAt          *time.Time `gorm:"index"`
	LastCommit          string     `gorm:"size:255;not null;default:''"`
	LastError           string     `gorm:"type:text;not null"`
	Info                string     `gorm:"column:info;type:json;not null"`
	LeaseOwner          string     `gorm:"size:128;not null;default:''"` // Unique build-attempt fence, not the human actor.
	LeaseUntil          *time.Time `gorm:"index"`
	Revision            uint64     `gorm:"not null;default:0"`
	CreatedBy           string     `gorm:"size:64;not null"`
	UpdatedBy           string     `gorm:"size:64;not null"`
	CreatedAt           time.Time  `gorm:"not null"`
	UpdatedAt           time.Time  `gorm:"not null;index"`
}

// TableName returns the stable public Git mirror registry table name.
func (Cache) TableName() string { return "public_repository_caches" }
