package models

import "time"

// Pipeline represents a workflow definition file stored in the repository.
type Pipeline struct {
	ID        int64     `xorm:"'id' pk autoincr"              json:"id"`
	RepoID    int64     `xorm:"'repo_id' notnull index"       json:"repoId"`
	Name      string    `xorm:"'name' varchar(255)"           json:"name"`
	FilePath  string    `xorm:"'file_path' varchar(500)"      json:"filePath"`
	CreatedAt time.Time `xorm:"'created_at' created"          json:"createdAt"`
	UpdatedAt time.Time `xorm:"'updated_at' updated"          json:"updatedAt"`
}

// PipelineRun is a single execution of a Pipeline.
type PipelineRun struct {
	ID          int64      `xorm:"'id' pk autoincr"              json:"id"`
	PipelineID  int64      `xorm:"'pipeline_id' notnull index"   json:"pipelineId"`
	RepoID      int64      `xorm:"'repo_id' notnull index"       json:"repoId"`
	TriggerRef  string     `xorm:"'trigger_ref' varchar(255)"    json:"triggerRef"`  // refs/heads/main
	TriggerSHA  string     `xorm:"'trigger_sha' varchar(40)"     json:"triggerSha"`
	TriggeredBy string     `xorm:"'triggered_by' varchar(64)"    json:"triggeredBy"`
	Status      string     `xorm:"'status' varchar(20)"          json:"status"` // queued/running/succeeded/failed/cancelled
	StartedAt   *time.Time `xorm:"'started_at'"                  json:"startedAt"`
	FinishedAt  *time.Time `xorm:"'finished_at'"                 json:"finishedAt"`
	CreatedAt   time.Time  `xorm:"'created_at' created"          json:"createdAt"`
}

// PipelineJob is a single node in the DAG for a PipelineRun.
type PipelineJob struct {
	ID         int64      `xorm:"'id' pk autoincr"              json:"id"`
	RunID      int64      `xorm:"'run_id' notnull index"        json:"runId"`
	Name       string     `xorm:"'name' varchar(255)"           json:"name"`
	Image      string     `xorm:"'image' varchar(500)"          json:"image"` // Docker image
	Needs      string     `xorm:"'needs' text"                  json:"needs"` // JSON array of dependency job names
	Status     string     `xorm:"'status' varchar(20)"          json:"status"`
	StartedAt  *time.Time `xorm:"'started_at'"                  json:"startedAt"`
	FinishedAt *time.Time `xorm:"'finished_at'"                 json:"finishedAt"`
	CreatedAt  time.Time  `xorm:"'created_at' created"          json:"createdAt"`
}

// PipelineStep is a single command within a PipelineJob.
type PipelineStep struct {
	ID         int64      `xorm:"'id' pk autoincr"              json:"id"`
	JobID      int64      `xorm:"'job_id' notnull index"        json:"jobId"`
	Seq        int        `xorm:"'seq'"                         json:"seq"`     // execution order within the job
	Name       string     `xorm:"'name' varchar(255)"           json:"name"`
	RunCmd     string     `xorm:"'run_cmd' text"                json:"runCmd"`  // shell command (run:)
	UsesAction string     `xorm:"'uses_action' varchar(255)"    json:"usesAction"` // built-in action (uses:)
	Status     string     `xorm:"'status' varchar(20)"          json:"status"`
	ExitCode   int        `xorm:"'exit_code'"                   json:"exitCode"`
	StartedAt  *time.Time `xorm:"'started_at'"                  json:"startedAt"`
	FinishedAt *time.Time `xorm:"'finished_at'"                 json:"finishedAt"`
}

// PipelineStepLog stores append-only log output for a step.
type PipelineStepLog struct {
	ID         int64     `xorm:"'id' pk autoincr"              json:"id"`
	StepID     int64     `xorm:"'step_id' notnull index"       json:"stepId"`
	ChunkIndex int       `xorm:"'chunk_index'"                 json:"chunkIndex"`
	Content    string    `xorm:"'content' text"                json:"content"`
	CreatedAt  time.Time `xorm:"'created_at' created"          json:"createdAt"`
}

// Runner is a registered execution backend.
type Runner struct {
	ID         int64     `xorm:"'id' pk autoincr"              json:"id"`
	Name       string    `xorm:"'name' unique varchar(100)"    json:"name"`
	Labels     string    `xorm:"'labels' text"                 json:"labels"`     // JSON array of capability labels
	Status     string    `xorm:"'status' varchar(20)"          json:"status"`     // idle/busy/offline
	TokenHash  string    `xorm:"'token_hash' varchar(64)"      json:"-"`
	LastSeenAt time.Time `xorm:"'last_seen_at'"                json:"lastSeenAt"`
	CreatedAt  time.Time `xorm:"'created_at' created"          json:"createdAt"`
}

// Artifact is a file produced by a PipelineRun and stored for later download.
type Artifact struct {
	ID          int64     `xorm:"'id' pk autoincr"              json:"id"`
	RunID       int64     `xorm:"'run_id' notnull index"        json:"runId"`
	RepoID      int64     `xorm:"'repo_id' notnull index"       json:"repoId"`
	Name        string    `xorm:"'name' varchar(255)"           json:"name"`
	StoragePath string    `xorm:"'storage_path' varchar(500)"   json:"-"`
	Size        int64     `xorm:"'size'"                        json:"size"`
	ContentType string    `xorm:"'content_type' varchar(100)"   json:"contentType"`
	CreatedAt   time.Time `xorm:"'created_at' created"          json:"createdAt"`
}
