package server

import (
	"encoding/json"
	"net/http"
	"strconv"
	"strings"
	"time"

	"gitlab.com/Timfa/bluegit/internal/access"
	"gitlab.com/Timfa/bluegit/internal/ci"
	"gitlab.com/Timfa/bluegit/internal/i18n"
	"gitlab.com/Timfa/bluegit/internal/store"
)

// PipeRow is één rij in het pipeline-overzicht.
type PipeRow struct {
	ID       int64
	Status   string
	Label    string
	Duration string
	When     string
	Title    string
	SHA      string
	Short    string
	Ref      string
	RefKind  string
	Actor    string
	Avatar   string
	Initial  string
	Stages   []StageView
}

// StageView is één bolletje in de stages-kolom.
type StageView struct {
	Name   string
	Status string
	Label  string
	Tip    string
	Jobs   []JobLink
}

// JobLink is een job in een stage-popover of in de zijbalk.
type JobLink struct {
	ID      int64
	Name    string
	Status  string
	Label   string
	Current bool
	Stage   string
}

// JobPage is de console van één job.
type JobPage struct {
	Job             store.Job
	Pipe            store.Pipeline
	Name            string
	Status          string
	Label           string
	Actor           string
	Avatar          string
	Initial         string
	When            string
	Duration        string
	Finished        string
	Queued          string
	Timeout         string
	Source          string
	Ref             string
	RefKind         string
	Short           string
	Title           string
	Stage           string
	PipeStatus      string
	PipeLabel       string
	Stages          []string
	Related         []JobLink
	Blocks          []LogBlock
	Running         bool
	LogBytes        int
	CanPlay         bool
	Environment     string
	EnvironmentURL  string
	EnvironmentLink string
}

// LogBlock is een losse regel of een inklapbare sectie.
type LogBlock struct {
	Section  bool
	Title    string
	Duration string
	Open     bool
	Lines    []LogLine
}

// LogLine is één regel in de console.
type LogLine struct {
	N     int
	Time  string
	Text  string
	Spans []LogSpan
}

func (s *Server) fillPipelines(r *http.Request, v *View, repo store.Repo) {
	lang := s.langOf(r)
	list, _ := s.Store.ListPipelines(r.Context(), repo.ID)
	q := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("q")))
	v.Query = r.URL.Query().Get("q")
	users := map[string]store.User{}
	for _, p := range list {
		row := s.pipeRow(r, lang, repo, p, users)
		if q != "" && !pipeMatches(row, q) {
			continue
		}
		v.PipeRows = append(v.PipeRows, row)
	}
}

func (s *Server) pipeRow(r *http.Request, lang i18n.Lang, repo store.Repo, p store.Pipeline, users map[string]store.User) PipeRow {
	jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
	name, kind := ci.SplitRef(p.Ref)
	status := displayStatus(p.Status, jobs)
	actor, avatar := s.actorOf(r, p.Actor, users)
	initial := ""
	if actor != "" {
		initial = strings.ToUpper(actor[:1])
	}
	end := p.FinishedAt
	if end.IsZero() {
		end = time.Now()
	}
	start := p.StartedAt
	if start.IsZero() {
		start = p.CreatedAt
	}
	return PipeRow{
		ID: p.ID, Status: status, Label: s.tr(r, "pipe."+status),
		Duration: formatClock(end.Sub(start)), When: i18n.Ago(lang, p.CreatedAt.Unix()),
		Title: p.CommitTitle, SHA: p.CommitSHA, Short: shortSHA(p.CommitSHA),
		Ref: name, RefKind: kind, Actor: actor, Avatar: avatar, Initial: initial,
		Stages: stageViews(lang, jobs, func(key string) string { return s.tr(r, key) }),
	}
}

func (s *Server) actorOf(r *http.Request, name string, cache map[string]store.User) (string, string) {
	name = strings.TrimSpace(name)
	if name == "" {
		return "", ""
	}
	u, ok := cache[name]
	if !ok {
		found, err := s.Store.UserByUsername(r.Context(), name)
		if err == nil {
			u = found
		}
		cache[name] = u
	}
	if u.ID == 0 {
		return name, ""
	}
	return u.Username, u.AvatarURL
}

func pipeMatches(row PipeRow, q string) bool {
	bits := []string{row.Label, row.Title, row.SHA, row.Short, row.Ref, row.Actor, strconv.FormatInt(row.ID, 10), row.Status}
	for _, st := range row.Stages {
		bits = append(bits, st.Name)
		for _, j := range st.Jobs {
			bits = append(bits, j.Name)
		}
	}
	for _, b := range bits {
		if strings.Contains(strings.ToLower(b), q) {
			return true
		}
	}
	return false
}

func displayStatus(pipe string, jobs []store.Job) string {
	switch pipe {
	case "failed":
		return "failed"
	case "running":
		return "running"
	case "manual":
		return "manual"
	case "queued":
		return "pending"
	case "canceled":
		return "canceled"
	}
	warn := false
	for _, j := range jobs {
		if j.Status == "failed" && j.AllowFailure {
			warn = true
		}
	}
	if warn {
		return "warning"
	}
	if pipe == "success" {
		return "passed"
	}
	if pipe == "" {
		return "pending"
	}
	return pipe
}

func stageViews(lang i18n.Lang, jobs []store.Job, tr func(string) string) []StageView {
	var order []string
	by := map[string][]store.Job{}
	for _, j := range jobs {
		if _, ok := by[j.Stage]; !ok {
			order = append(order, j.Stage)
		}
		by[j.Stage] = append(by[j.Stage], j)
	}
	var out []StageView
	for _, name := range order {
		group := by[name]
		status := stageStatus(group)
		label := tr("pipe." + status)
		var links []JobLink
		for _, j := range group {
			st := jobDisplay(j)
			links = append(links, JobLink{ID: j.ID, Name: j.Name, Status: st, Label: tr("pipe." + st), Stage: j.Stage})
		}
		out = append(out, StageView{Name: name, Status: status, Label: label, Tip: name + ": " + strings.ToLower(label), Jobs: links})
	}
	_ = lang
	return out
}

func stageStatus(jobs []store.Job) string {
	pending, failed, warning, running, canceled := false, false, false, false, false
	seen := false
	for _, j := range jobs {
		seen = true
		switch {
		case j.Status == "running":
			running = true
		case j.Status == "failed" && !j.AllowFailure:
			failed = true
		case j.Status == "canceled" && !j.AllowFailure:
			canceled = true
		case j.Status == "failed" && j.AllowFailure:
			warning = true
		case j.Status == "queued" || j.Status == "manual":
			pending = true
		}
	}
	if !seen {
		return "skipped"
	}
	if running {
		return "running"
	}
	if failed {
		return "failed"
	}
	if canceled {
		return "canceled"
	}
	if warning {
		return "warning"
	}
	if pending {
		return "pending"
	}
	allSkip := true
	for _, j := range jobs {
		if j.Status != "skipped" {
			allSkip = false
		}
	}
	if allSkip {
		return "skipped"
	}
	return "passed"
}

func jobDisplay(j store.Job) string {
	if j.Status == "failed" && j.AllowFailure {
		return "warning"
	}
	switch j.Status {
	case "success":
		return "passed"
	case "queued":
		return "pending"
	case "":
		return "pending"
	default:
		return j.Status
	}
}

func formatClock(d time.Duration) string {
	if d < 0 {
		d = 0
	}
	s := int(d.Seconds())
	return pad2(s/3600) + ":" + pad2((s%3600)/60) + ":" + pad2(s%60)
}

func pad2(n int) string {
	if n < 10 {
		return "0" + strconv.Itoa(n)
	}
	return strconv.Itoa(n)
}

func shortSHA(sha string) string {
	if len(sha) > 8 {
		return sha[:8]
	}
	return sha
}

func (s *Server) showJob(w http.ResponseWriter, r *http.Request) {
	_, repo, role, ok := s.guard(w, r, access.RoleReadonly)
	if !ok {
		return
	}
	p, job, ok := s.jobFromRequest(w, r, repo)
	if !ok {
		return
	}
	v := s.repoView(w, r, repo, role, job.Name)
	v.Wide = true
	v.Page = "pipelines"
	v.Pipe = &p
	v.JobPage = s.jobPage(r, repo, p, job, role)
	s.render(w, "job", v)
}

func (s *Server) jobLog(w http.ResponseWriter, r *http.Request) {
	_, repo, _, ok := s.guard(w, r, access.RoleReadonly)
	if !ok {
		return
	}
	_, job, ok := s.jobFromRequest(w, r, repo)
	if !ok {
		return
	}
	if r.URL.Query().Get("raw") == "1" {
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
		_, _ = w.Write([]byte(job.Log))
		return
	}
	offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
	if offset < 0 || offset > len(job.Log) {
		offset = 0
	}
	w.Header().Set("Content-Type", "application/json")
	_ = json.NewEncoder(w).Encode(map[string]any{
		"offset": len(job.Log),
		"status": job.Status,
		"text":   job.Log[offset:],
	})
}

func (s *Server) playJob(w http.ResponseWriter, r *http.Request) {
	_, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
	if !ok || !s.csrfOK(w, r) {
		return
	}
	p, job, ok := s.jobFromRequest(w, r, repo)
	if !ok {
		return
	}
	_, _ = s.Store.QueueJob(r.Context(), job.ID)
	s.redirect(w, r, repoURL(repo)+"/-/pipelines/"+strconv.FormatInt(p.ID, 10)+"/jobs/"+strconv.FormatInt(job.ID, 10), s.tr(r, "flash.pipeline"), "")
}

func (s *Server) jobFromRequest(w http.ResponseWriter, r *http.Request, repo store.Repo) (store.Pipeline, store.Job, bool) {
	pid, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
	if err != nil {
		http.NotFound(w, r)
		return store.Pipeline{}, store.Job{}, false
	}
	jid, err := strconv.ParseInt(r.PathValue("job"), 10, 64)
	if err != nil {
		http.NotFound(w, r)
		return store.Pipeline{}, store.Job{}, false
	}
	p, err := s.Store.PipelineByID(r.Context(), repo.ID, pid)
	if err != nil {
		http.NotFound(w, r)
		return store.Pipeline{}, store.Job{}, false
	}
	jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
	for _, j := range jobs {
		if j.ID == jid {
			return p, j, true
		}
	}
	http.NotFound(w, r)
	return store.Pipeline{}, store.Job{}, false
}

func (s *Server) jobPage(r *http.Request, repo store.Repo, p store.Pipeline, job store.Job, role access.Role) *JobPage {
	lang := s.langOf(r)
	jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
	users := map[string]store.User{}
	actor, avatar := s.actorOf(r, p.Actor, users)
	initial := ""
	if actor != "" {
		initial = strings.ToUpper(actor[:1])
	}
	name, kind := ci.SplitRef(p.Ref)
	status := jobDisplay(job)
	pipeStatus := displayStatus(p.Status, jobs)
	end := job.FinishedAt
	if end.IsZero() && (job.Status == "success" || job.Status == "failed" || job.Status == "skipped") {
		end = p.FinishedAt
	}
	start := job.StartedAt
	dur := ""
	if !start.IsZero() {
		until := end
		if until.IsZero() {
			until = time.Now()
		}
		dur = formatClock(until.Sub(start))
	}
	queued := ""
	if !start.IsZero() && !p.CreatedAt.IsZero() {
		queued = formatClock(start.Sub(p.CreatedAt))
	}
	timeout := formatClock(job.Timeout)
	if job.Timeout == 0 && s.Cfg.CI.TimeoutSec > 0 {
		timeout = formatClock(time.Duration(s.Cfg.CI.TimeoutSec) * time.Second)
	}
	finished := ""
	if !end.IsZero() {
		finished = i18n.Ago(lang, end.Unix())
	}
	var stages []string
	seen := map[string]bool{}
	var related []JobLink
	for _, j := range jobs {
		if !seen[j.Stage] {
			seen[j.Stage] = true
			stages = append(stages, j.Stage)
		}
		st := jobDisplay(j)
		related = append(related, JobLink{ID: j.ID, Name: j.Name, Status: st, Label: s.tr(r, "pipe."+st), Current: j.ID == job.ID, Stage: j.Stage})
	}
	sourceKey := "pipe.src." + p.Source
	source := s.tr(r, sourceKey)
	if source == sourceKey {
		source = p.Source
	}
	return &JobPage{
		Job: job, Pipe: p, Name: job.Name, Status: status, Label: s.tr(r, "pipe."+status),
		Actor: actor, Avatar: avatar, Initial: initial, When: i18n.Ago(lang, p.CreatedAt.Unix()),
		Duration: dur, Finished: finished, Queued: queued, Timeout: timeout, Source: source,
		Ref: name, RefKind: kind, Short: shortSHA(p.CommitSHA), Title: p.CommitTitle, Stage: job.Stage,
		PipeStatus: pipeStatus, PipeLabel: s.tr(r, "pipe."+pipeStatus),
		Stages: stages, Related: related, Blocks: parseLog(job.Log), Running: job.Status == "running" || job.Status == "queued",
		LogBytes: len(job.Log), CanPlay: job.Status == "manual" && role.CanWriteCode(),
		Environment: job.Environment, EnvironmentURL: job.EnvironmentURL, EnvironmentLink: safeEnvironmentURL(job.EnvironmentURL),
	}
}

func safeEnvironmentURL(raw string) string {
	if strings.Contains(raw, "$") {
		return ""
	}
	if strings.HasPrefix(raw, "https://") || strings.HasPrefix(raw, "http://") {
		return raw
	}
	return ""
}

func parseLog(raw string) []LogBlock {
	var blocks []LogBlock
	var cur *LogBlock
	n := 0
	flush := func() {
		if cur == nil {
			return
		}
		blocks = append(blocks, *cur)
		cur = nil
	}
	for _, row := range strings.Split(raw, "\n") {
		if row == "" {
			continue
		}
		ts, text := row, row
		if i := strings.IndexByte(row, '\t'); i >= 0 {
			ts, text = row[:i], row[i+1:]
		}
		if strings.HasPrefix(text, "section_start:") {
			flush()
			body := strings.TrimPrefix(text, "section_start:")
			title := body
			if i := strings.IndexByte(body, ':'); i >= 0 {
				title = body[i+1:]
			}
			cur = &LogBlock{Section: true, Title: title, Open: true}
			continue
		}
		if strings.HasPrefix(text, "section_end:") {
			dur := ""
			body := strings.TrimPrefix(text, "section_end:")
			if i := strings.IndexByte(body, '\t'); i >= 0 {
				dur = body[i+1:]
			}
			if cur != nil {
				cur.Duration = dur
				flush()
			}
			continue
		}
		n++
		line := LogLine{N: n, Time: clockOf(ts), Text: text, Spans: ansiSpans(text)}
		if cur != nil {
			cur.Lines = append(cur.Lines, line)
			continue
		}
		blocks = append(blocks, LogBlock{Lines: []LogLine{line}})
	}
	if cur != nil {
		flush()
	}
	return blocks
}

func clockOf(ts string) string {
	if t, err := time.Parse(time.RFC3339, ts); err == nil {
		return t.UTC().Format("15:04:05")
	}
	if len(ts) >= 19 && strings.Contains(ts, "T") {
		return ts[11:19]
	}
	return ""
}
