package models

import "time"

// BuildStatus represents the status of a build job.
type BuildStatus string

const (
	BuildStatusPending   BuildStatus = "pending"
	BuildStatusFetching  BuildStatus = "fetching"
	BuildStatusBuilding  BuildStatus = "building"
	BuildStatusPushing   BuildStatus = "pushing"
	BuildStatusSuccess   BuildStatus = "success"
	BuildStatusFailed    BuildStatus = "failed"
	BuildStatusCancelled BuildStatus = "cancelled"
)

// BuildRequest represents the information needed to initiate a build.
type BuildRequest struct {
	ComponentID          uint              `json:"component_id"`
	Version              string            `json:"version"`      // e.g., git commit hash, tag, or branch
	SourceURL            string            `json:"source_url"`   // Git repository URL
	RegistryURL          string            `json:"registry_url"` // Target Docker registry URL
	RegistryUser         string            `json:"registry_user,omitempty"`
	RegistryPassword     string            `json:"registry_password,omitempty"`
	ImageName            string            `json:"image_name"`                       // Name of the image to build (without tag)
	BuildContext         string            `json:"build_context"`                    // Path to the Dockerfile within the repo, default "."
	Dockerfile           string            `json:"dockerfile"`                       // Path to the Dockerfile, default "Dockerfile"
	NoCache              bool              `json:"no_cache"`                         // Whether to use --no-cache for the build
	BuildArgs            map[string]string `json:"build_args"`                       // Build-time variables
	Source               string            `json:"source,omitempty"`                 // "autodetect", "dockerfile", "docker-compose"
	DockerfileContent    string            `json:"dockerfile_content,omitempty"`     // Generated or user-provided Dockerfile content
	DockerComposeContent string            `json:"docker_compose_content,omitempty"` // docker-compose.yml content
}

// BuildJob represents a build job in the system.
// This will correspond to a 'build_jobs' table in the database.
type BuildJob struct {
	ID                uint        `json:"id" gorm:"primaryKey;autoIncrement"`
	ComponentID       uint        `json:"component_id" gorm:"not null;index"`
	RequestID         string      `json:"request_id" gorm:"uniqueIndex"` // A unique ID for idempotency if needed
	SourceURL         string      `json:"source_url" gorm:"not null"`
	Version           string      `json:"version"`
	Status            BuildStatus `json:"status" gorm:"not null;index"`
	ImageName         string      `json:"image_name"`     // e.g., myapp
	ImageTag          string      `json:"image_tag"`      // e.g., v1.0.0-commitsha
	FullImageURI      string      `json:"full_image_uri"` // e.g., registry.example.com/myapp:v1.0.0-commitsha
	RegistryURL       string      `json:"registry_url"`
	RegistryUser      string      `json:"registry_user,omitempty"`
	RegistryPassword  string      `json:"registry_password,omitempty"` // Consider how to store this securely if at all long-term
	BuildContext      string      `json:"build_context"`
	Dockerfile        string      `json:"dockerfile"`
	Source            string      `json:"source,omitempty"` // "autodetect", "dockerfile", "docker-compose"
	NoCache           bool        `json:"no_cache"`
	BuildArgs         string      `json:"build_args" gorm:"type:text"`                   // Stored as JSON string or similar
	DockerfileContent string      `json:"dockerfile_content,omitempty" gorm:"type:text"` // Generated Dockerfile content
	Logs              string      `json:"logs" gorm:"type:text"`
	ErrorMessage      string      `json:"error_message"`
	RequestedAt       time.Time   `json:"requested_at" gorm:"not null;index"`
	StartedAt         *time.Time  `json:"started_at,omitempty"`
	FinishedAt        *time.Time  `json:"finished_at,omitempty"`
	WorkerNodeID      string      `json:"worker_node_id,omitempty"` // ID of the build machine that processed/is processing this job
	CreatedAt         time.Time   `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt         time.Time   `json:"updated_at" gorm:"autoUpdateTime"`
}
