package models

import "time"

// GitOpsConfig links an Environment to a branch that serves as its desired state.
// When the HEAD SHA of Branch diverges from ActualSHA, the environment is "drifted".
type GitOpsConfig struct {
	ID            int64      `xorm:"'id' pk autoincr"                      json:"id"`
	EnvID         int64      `xorm:"'env_id' unique notnull index"          json:"envId"` // one config per env
	RepoID        int64      `xorm:"'repo_id' notnull index"               json:"repoId"`
	Branch        string     `xorm:"'branch' varchar(255) notnull"          json:"branch"`       // source-of-truth branch
	AutoSync      bool       `xorm:"'auto_sync' default false"              json:"autoSync"`     // create deployment on drift
	SyncInterval  int        `xorm:"'sync_interval' default 0"              json:"syncInterval"` // seconds; 0 = push-only
	SyncStatus    string     `xorm:"'sync_status' varchar(20) default 'unknown'" json:"syncStatus"` // unknown/synced/drifted/syncing
	DesiredSHA    string     `xorm:"'desired_sha' varchar(40)"              json:"desiredSha"` // last known branch HEAD
	ActualSHA     string     `xorm:"'actual_sha' varchar(40)"               json:"actualSha"`  // SHA of last successful deploy
	LastCheckedAt *time.Time `xorm:"'last_checked_at'"                      json:"lastCheckedAt"`
	CreatedAt     time.Time  `xorm:"'created_at' created"                   json:"createdAt"`
	UpdatedAt     time.Time  `xorm:"'updated_at' updated"                   json:"updatedAt"`
}

// GitOpsDriftEvent is an append-only record of each drift detection and its resolution.
type GitOpsDriftEvent struct {
	ID         int64      `xorm:"'id' pk autoincr"              json:"id"`
	EnvID      int64      `xorm:"'env_id' notnull index"        json:"envId"`
	RepoID     int64      `xorm:"'repo_id' notnull index"       json:"repoId"`
	DesiredSHA string     `xorm:"'desired_sha' varchar(40)"     json:"desiredSha"` // SHA that should be deployed
	ActualSHA  string     `xorm:"'actual_sha' varchar(40)"      json:"actualSha"`  // SHA actually deployed (empty = never)
	SyncStatus string     `xorm:"'sync_status' varchar(20)"     json:"syncStatus"` // drifted/synced/acknowledged
	DetectedAt time.Time  `xorm:"'detected_at' notnull index"   json:"detectedAt"`
	ResolvedAt *time.Time `xorm:"'resolved_at'"                 json:"resolvedAt"`
}
