package server

import (
	"context"
	"encoding/json"
	"errors"
	"html/template"
	"net/http"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"

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

const ciFile = "blue-ci-cd.yml"
const ciMaxBytes = 200_000

// ciTemplate is het startbestand als blue-ci-cd.yml nog niet bestaat.
// Alles staat in commentaar, zodat opslaan geen job en geen mislukte pipeline maakt.
const ciTemplate = `# blue-ci-cd.yml
# Uncomment the job to run it.
#
# stages:
#   - test
#
# unit:
#   stage: test
#   image: alpine:3.20
#   script:
#     - echo hello
`

// CIEditor is de Pipeline Editor voor blue-ci-cd.yml.
type CIEditor struct {
	Ref      string
	Body     string
	Rows     []EditorLine
	Issues   []LintRow
	Errors   int
	Warnings int
	Status   string
	Class    string
	Missing  bool
	NeedsMR  bool
	Suggest  string
}

// EditorLine is één gekleurde regel, met een markering als de linter daar klaagt.
type EditorLine struct {
	N    int
	HTML template.HTML
	Bad  bool
}

// LintRow is één melding onder de editor.
type LintRow struct {
	Line    int    `json:"line"`
	Text    string `json:"text"`
	Warning bool   `json:"warning"`
}

func (s *Server) ciEditor(w http.ResponseWriter, r *http.Request) {
	_, repo, role, ok := s.guard(w, r, access.RoleReadonly)
	if !ok {
		return
	}
	ref := strings.TrimSpace(r.URL.Query().Get("ref"))
	if ref == "" {
		ref = r.URL.Query().Get("rev")
	}
	ref = safeRev(ref, repo.DefaultBranch)
	v := s.repoView(w, r, repo, role, "edit.title")
	v.Wide = true
	v.Page = "editor"
	v.Rev = ref
	dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
	s.branchList(r, &v, repo)
	body, missing := s.ciFileAt(r, dir, ref)
	if missing {
		body = ciTemplate
	}
	model := s.ciEditorModel(r, body, s.includeFrom(r, dir, ref))
	model.Ref = ref
	model.Missing = missing
	patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID)
	model.NeedsMR = access.BranchProtected(patterns, ref) && !role.CanPushBranch(true)
	if model.NeedsMR {
		model.Suggest = s.suggestCIBranch(r, dir, patterns)
	}
	v.Editor = model
	s.render(w, "editor", v)
}

func (s *Server) lintCI(w http.ResponseWriter, r *http.Request) {
	_, repo, _, ok := s.guard(w, r, access.RoleReadonly)
	if !ok || !s.csrfOK(w, r) {
		return
	}
	out, err := s.opLintCI(r, repo, r.FormValue("body"), r.FormValue("ref"))
	if err != nil {
		http.Error(w, s.trErr(r, err), statusOf(err))
		return
	}
	w.Header().Set("Content-Type", "application/json")
	_ = json.NewEncoder(w).Encode(out)
}

func (s *Server) saveCIEditor(w http.ResponseWriter, r *http.Request) {
	u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
	if !ok || !s.csrfOK(w, r) {
		return
	}
	branch := strings.TrimSpace(r.FormValue("branch"))
	base := r.FormValue("base")
	back := repoURL(repo) + "/-/ci/editor?ref=" + url.QueryEscape(branch)
	openMR := r.FormValue("open_mr") == "1"
	_, number, err := s.opSaveCI(r.Context(), u, repo, base, branch, r.FormValue("message"), r.FormValue("body"), openMR)
	if err != nil {
		if strings.TrimSpace(branch) == "" || !validBranchName(branch) {
			back = repoURL(repo) + "/-/ci/editor?ref=" + url.QueryEscape(safeRev(base, repo.DefaultBranch))
		}
		s.redirect(w, r, back, "", s.commitProblem(r, err))
		return
	}
	if openMR && number > 0 {
		s.redirect(w, r, repoURL(repo)+"/-/merge-requests/"+strconv.FormatInt(number, 10), s.tr(r, "flash.mrOpened"), "")
		return
	}
	s.redirect(w, r, back, s.tr(r, "edit.saved"), "")
}

// openCIMergeRequest opent een merge request van branch naar target.
// Een open verzoek met dezelfde bronbranch wordt hergebruikt.
func (s *Server) openCIMergeRequest(r *http.Request, repo store.Repo, author store.User, dir, branch, target, title string) (int64, error) {
	existing, err := s.Store.OpenMergeRequestsBySource(r.Context(), repo.ID, branch)
	if err != nil {
		return 0, err
	}
	if len(existing) > 0 {
		return existing[0].Number, nil
	}
	mr, err := s.Store.CreateMergeRequest(r.Context(), repo.ID, author.ID, title, "", branch, target)
	if err != nil {
		return 0, err
	}
	if sha := gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "refs/heads/"+branch); sha != "" {
		s.startOneMergePipeline(r.Context(), repo, sha, author.Username, "", mr)
	}
	s.mrHook(repo, mr, author.Username, "opened", "")
	s.recordContribution(author.ID, repo.ID, "merge_request", mrRef(mr.ID), mr.CreatedAt)
	return mr.Number, nil
}

func (s *Server) ciEditorModel(r *http.Request, body string, read func(string) ([]byte, error)) *CIEditor {
	_, issues := ci.Lint([]byte(body), read)
	bad := map[int]bool{}
	var rows []LintRow
	errors, warnings := 0, 0
	for _, is := range issues {
		line := is.Line
		if line < 1 {
			line = 1
		}
		text := s.tr(r, is.Key, is.Args...)
		rows = append(rows, LintRow{Line: line, Text: text, Warning: is.Warning})
		if is.Warning {
			warnings++
			continue
		}
		errors++
		bad[line] = true
	}
	painted := highlight.Lines(ciFile, body)
	if len(painted) == 0 {
		painted = []string{""}
	}
	if strings.HasSuffix(body, "\n") {
		painted = append(painted, "")
	}
	lines := make([]EditorLine, len(painted))
	for i, htmlLine := range painted {
		n := i + 1
		lines[i] = EditorLine{N: n, HTML: template.HTML(htmlLine), Bad: bad[n]}
	}
	status, class := s.tr(r, "edit.clean"), "passed"
	if errors > 0 {
		key := "edit.issues"
		if errors == 1 {
			key = "edit.issueOne"
		}
		status, class = s.tr(r, key, errors), "failed"
	} else if warnings > 0 {
		key := "edit.warnings"
		if warnings == 1 {
			key = "edit.warningOne"
		}
		status, class = s.tr(r, key, warnings), "warning"
	}
	if rows == nil {
		rows = []LintRow{}
	}
	return &CIEditor{
		Body: body, Rows: lines, Issues: rows,
		Errors: errors, Warnings: warnings, Status: status, Class: class,
	}
}

func (s *Server) ciFileAt(r *http.Request, dir, ref string) (string, bool) {
	out, err := exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+ciFile).Output()
	if err != nil {
		return "", true
	}
	if len(out) > ciMaxBytes {
		return "", false
	}
	return string(out), false
}

func (s *Server) suggestCIBranch(r *http.Request, dir string, patterns []string) string {
	const base = "add-blue-ci-cd"
	for n := 1; n < 100; n++ {
		name := base
		if n > 1 {
			name = base + "-" + strconv.Itoa(n)
		}
		if access.BranchProtected(patterns, name) {
			continue
		}
		if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+name) == "" {
			return name
		}
	}
	return base
}

func (s *Server) includeFrom(r *http.Request, dir, ref string) func(string) ([]byte, error) {
	return func(path string) ([]byte, error) {
		if !validInclude(path) {
			return nil, os.ErrInvalid
		}
		return exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+path).Output()
	}
}

func (s *Server) ensureBranch(ctx context.Context, dir, branch, base, work string) error {
	home := filepath.Join(work, "home")
	if err := os.MkdirAll(home, 0o700); err != nil {
		return gitrepo.Cause("edit.failWrite", err.Error())
	}
	env := gitrepo.CommandEnv("bluegit", "bluegit@users.git.timfalken.com", home, home)
	if s.gitEnv(ctx, dir, env, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch) != "" {
		return nil
	}
	if s.gitEnv(ctx, dir, env, "rev-parse", "--verify", "--quiet", "refs/heads/"+base) != "" {
		cmd := exec.CommandContext(ctx, s.Git, "-C", dir, "branch", branch, base)
		cmd.Env = env
		if out, err := cmd.CombinedOutput(); err != nil {
			return gitrepo.Cause("edit.failBranch", strings.TrimSpace(string(out)))
		}
		return nil
	}
	// Nog geen enkele commit. De gekozen branch wordt de eerste, zonder parent.
	// Een beheerder, ook de eigenaar, mag dat ook op een beschermde branch.
	if s.gitEnv(ctx, dir, env, "rev-parse", "--verify", "--quiet", "HEAD") == "" {
		return nil
	}
	return i18n.E("edit.failBranch")
}

func (s *Server) gitEnv(ctx context.Context, dir string, env []string, args ...string) string {
	bin := s.Git
	if bin == "" {
		bin = "git"
	}
	cmd := exec.CommandContext(ctx, bin, append([]string{"-C", dir}, args...)...)
	cmd.Env = env
	out, err := cmd.Output()
	if err != nil {
		return ""
	}
	return strings.TrimSpace(string(out))
}

// scratchDir is een werkmap onder de state (/var/lib/bluegit/tmp of STATE_DIRECTORY).
// /tmp en de homedirectory van de service zijn dat niet: ProtectSystem en ProtectHome sluiten die.
func (s *Server) scratchDir(prefix string) (string, error) {
	root := strings.TrimSpace(s.StateDir)
	if root == "" {
		root = strings.TrimSpace(os.Getenv("STATE_DIRECTORY"))
	}
	if root == "" {
		root = strings.TrimSpace(s.StoragePath())
	}
	if root == "" {
		return "", i18n.E("edit.failWrite")
	}
	dir := filepath.Join(root, "tmp")
	if err := os.MkdirAll(dir, 0o750); err != nil {
		return "", gitrepo.Cause("edit.failWrite", err.Error())
	}
	d, err := os.MkdirTemp(dir, prefix+"-")
	if err != nil {
		return "", gitrepo.Cause("edit.failWrite", err.Error())
	}
	return d, nil
}

// commitProblem logt de ruwe git-fout en geeft de vertaalde reden voor de pagina.
func (s *Server) commitProblem(r *http.Request, err error) string {
	if err != nil && s.Log != nil {
		s.Log.Error("git-commit", "err", err)
	}
	var ie i18n.Error
	if errors.As(err, &ie) {
		return s.trErr(r, err)
	}
	return s.tr(r, "edit.failGit")
}

func validBranchName(s string) bool {
	if s == "" || s == "@" || strings.HasPrefix(s, "-") || strings.HasPrefix(s, "/") || strings.HasSuffix(s, "/") || strings.HasSuffix(s, ".") || strings.Contains(s, "..") || strings.Contains(s, "//") || strings.Contains(s, "@{") {
		return false
	}
	return !strings.ContainsAny(s, " \t\n~^:?*[\\")
}

func validInclude(p string) bool {
	if p == "" || strings.HasPrefix(p, "/") || strings.Contains(p, `\`) || strings.Contains(p, "..") {
		return false
	}
	return true
}
