package ci

import "time"

// Pipeline is een geparset blue-ci-cd.yml.
type Pipeline struct {
	Stages    []string
	Variables map[string]string
	Image     string
	Before    []string
	After     []string
	Cache     Cache
	Timeout   time.Duration
	Tags      []string
	Jobs      []Job
	Warnings  []Issue
	Workflow  []Rule
}

// Job is één containerstap.
type Job struct {
	Name          string
	Stage         string
	Image         string
	Before        []string
	Script        []string
	After         []string
	Needs         []string
	Optional      []string
	NeedsSet      bool
	Variables     map[string]string
	Artifacts     Artifacts
	Rules         []Rule
	AllowFailure  bool
	When          string
	Timeout       time.Duration
	Tags          []string
	Cache         Cache
	ResourceGroup string
	Only          []string
	Except        []string
	Retry         Retry
	Parallel      int
	Matrix        []MatrixSpec
	Services      []Service
	Environment   Environment
	Interruptible bool
	// DoNotInterrupt: nooit automatisch halverwege stoppen (doNotInterrupt).
	DoNotInterrupt bool
}

// Retry is het aantal extra pogingen na een mislukking. Max is hoogstens 2.
// Een lege When betekent elke mislukking behalve een annulering.
type Retry struct {
	Max  int
	When []string
}

// MatrixSpec is één item uit parallel:matrix. Keys staan in YAML-volgorde.
type MatrixSpec struct {
	Keys []string
	Vals [][]string
}

// Service is een sidecar-container op hetzelfde Docker-netwerk als de job.
type Service struct {
	Name       string
	Alias      string
	Command    []string
	Entrypoint []string
	Variables  map[string]string
}

// Environment is de naam en een optionele URL. Er is geen deployment.
type Environment struct {
	Name string
	URL  string
}

// Artifacts zijn paden die na een geslaagde job bewaard blijven.
type Artifacts struct {
	Paths    []string
	ExpireIn time.Duration
}

// Cache is een map die tussen jobs van één repository gedeeld wordt.
type Cache struct {
	Key   string
	Paths []string
}

// Rule is één voorwaarde. De eerste rule die klopt wint.
// Zonder rules draait de job, tenzij only of except dat tegenhoudt.
type Rule struct {
	If      string
	Changes []string
	Exists  []string
	When    string
}

// Issue is een fout of een waarschuwing met een regelnummer.
type Issue struct {
	Line    int
	Key     string
	Args    []any
	Warning bool
}

// Eval is de context waarin rules worden beoordeeld.
// Changed == nil betekent dat de diff onbekend is; changes klopt dan.
// Files == nil betekent dat de boom onbekend is; exists klopt dan.
type Eval struct {
	Env     map[string]string
	Changed []string
	Files   []string
	// workflowMR: workflow:rules staat merge-request-pipelines expliciet
	// toe. Jobs zonder rules of only draaien dan ook daarin.
	workflowMR bool
}

// MRInfo is de merge request waar een pipeline bij hoort.
// Leeg betekent een gewone branch- of tag-pipeline.
type MRInfo struct {
	ID           int64
	IID          int64
	Title        string
	SourceBranch string
	TargetBranch string
	ProjectPath  string
	ProjectURL   string
	TargetSHA    string
}

// Ref beschrijft de commit waarvoor voorgedefinieerde variabelen gelden.
type Ref struct {
	SHA           string
	FullRef       string
	Name          string
	Kind          string
	Title         string
	Timestamp     string // committer-tijd, ISO 8601
	DefaultBranch string
	Source        string
	ProjectPath   string
	ProjectURL    string
	APIURL        string
	PipelineID    int64
	MR            *MRInfo
}
