package server

import (
	"context"
	"net/http"
	"os"
	"strconv"
	"strings"
	"time"

	"gitlab.com/Timfa/bluegit/internal/gitrepo"
	"gitlab.com/Timfa/bluegit/internal/state"
	"gitlab.com/Timfa/bluegit/internal/store"
)

// dirUsage is de laatst getelde grootte van de gekozen map.
type dirUsage struct {
	path    string
	n       uint64
	at      time.Time
	running bool
}

var (
	// usageTTL is hoe lang een volledige telling geldig blijft.
	usageTTL = 5 * time.Minute
	// usageWait is hoe lang de pagina op een nieuwe telling wacht.
	usageWait = 2 * time.Second
	// usageLimit stopt een telling die vastloopt.
	usageLimit = 15 * time.Minute
)

// bluegitUsage geeft de grootte van de map. De telling draait op de
// achtergrond en is altijd volledig; de pagina wacht hooguit usageWait.
// Is er dan nog geen uitkomst, dan geeft counting true en, als die er is,
// de vorige uitkomst. ok is false als er nog niets te tonen valt.
func (s *Server) bluegitUsage(path string) (n uint64, counting bool, ok bool) {
	s.usageMu.Lock()
	cur := s.usage
	if cur.path != path {
		cur = dirUsage{path: path}
		s.usage = cur
	}
	stale := cur.at.IsZero() || time.Since(cur.at) >= usageTTL
	var done chan struct{}
	if stale && !cur.running {
		s.usage.running = true
		done = make(chan struct{})
		s.usageDone = done
		go s.countUsage(path, done)
	} else if cur.running {
		done = s.usageDone
	}
	s.usageMu.Unlock()
	if stale && done != nil {
		select {
		case <-done:
		case <-time.After(usageWait):
		}
	}
	s.usageMu.Lock()
	defer s.usageMu.Unlock()
	cur = s.usage
	if cur.path != path {
		return 0, true, false
	}
	if cur.at.IsZero() {
		return 0, cur.running, false
	}
	return cur.n, cur.running, true
}

func (s *Server) countUsage(path string, done chan struct{}) {
	defer close(done)
	ctx, cancel := context.WithTimeout(context.Background(), usageLimit)
	defer cancel()
	n, err := state.DirSize(ctx, path)
	s.usageMu.Lock()
	defer s.usageMu.Unlock()
	if s.usage.path != path {
		return
	}
	s.usage.running = false
	if err != nil {
		return
	}
	s.usage.n = n
	s.usage.at = time.Now()
}

func (s *Server) noStorageText(r *http.Request) string {
	return s.tr(r, "err.noStorage") + "\n"
}

func allowsWithoutStorage(r *http.Request) bool {
	switch r.URL.Path {
	case "/healthz", "/login", "/auth/callback", "/logout", "/setup", "/-/info", "/api/v1/ci/spec", "/api/v1/ci/presets", "/api/v1/openapi.json", "/api/v1/webhooks/openapi.json", "/api/v1/admin/storage", "/api/v1/admin/self-update":
		return true
	}
	return strings.HasPrefix(r.URL.Path, "/assets/")
}

func isGitPath(path string) bool {
	return strings.HasSuffix(path, "/info/refs") ||
		strings.HasSuffix(path, "/git-upload-pack") ||
		strings.HasSuffix(path, "/git-receive-pack")
}

func (s *Server) gate(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if allowsWithoutStorage(r) || s.StoragePath() != "" {
			next.ServeHTTP(w, r)
			return
		}
		if isGitPath(r.URL.Path) || strings.HasPrefix(r.URL.Path, "/api/") {
			http.Error(w, s.noStorageText(r), http.StatusServiceUnavailable)
			return
		}
		if p := s.who(r); p.OK && p.User.InstanceAdmin {
			http.Redirect(w, r, "/setup", http.StatusFound)
			return
		}
		next.ServeHTTP(w, r)
	})
}

// StoragePath is de datadisk. Leeg zolang de beheerder er geen heeft gekozen.
func (s *Server) StoragePath() string {
	s.storageMu.RLock()
	set, path := s.storageSet, s.storage
	s.storageMu.RUnlock()
	if set {
		return path
	}
	s.storageMu.Lock()
	defer s.storageMu.Unlock()
	if !s.storageSet {
		s.storage = s.Cfg.StoragePath
		s.storageSet = true
	}
	return s.storage
}

// AdoptStorage zet het pad dat bij het starten al geldig was.
func (s *Server) AdoptStorage(path string) {
	s.storageMu.Lock()
	s.storage = path
	s.storageSet = true
	s.storageMu.Unlock()
	if path != "" && s.AfterStorage != nil {
		s.AfterStorage(path)
	}
}

func (s *Server) setupPage(w http.ResponseWriter, r *http.Request) {
	if _, ok := s.adminOnly(w, r); !ok {
		return
	}
	s.renderSetup(w, r, "", "")
}

func (s *Server) setupSave(w http.ResponseWriter, r *http.Request) {
	if _, ok := s.adminOnly(w, r); !ok || !s.csrfOK(w, r) {
		return
	}
	info, moved, err := s.opSetStorage(r.FormValue("path"))
	if err != nil {
		s.renderSetup(w, r, s.trErr(r, err), strings.TrimSpace(r.FormValue("path")))
		return
	}
	notice := s.tr(r, "err.storageSet", info.Path, state.Format(info.Free))
	if moved {
		notice = s.tr(r, "err.storageMoved", info.Path, state.Format(info.Free))
	}
	s.redirect(w, r, "/", notice, "")
}

func samePath(stateDir, path string) (bool, error) {
	if stateDir == "" {
		return false, nil
	}
	return strings.TrimRight(stateDir, "/") == strings.TrimRight(path, "/"), nil
}

func (s *Server) adminOnly(w http.ResponseWriter, r *http.Request) (bool, bool) {
	u, ok := s.loginRequired(w, r)
	if !ok {
		return false, false
	}
	if !u.InstanceAdmin {
		http.Error(w, s.tr(r, "err.noStorageAdmin"), http.StatusForbidden)
		return false, false
	}
	return true, true
}

func (s *Server) renderSetup(w http.ResponseWriter, r *http.Request, problem, prefill string) {
	v := s.view(w, r, "title.storage")
	v.Page = "setup"
	if problem != "" {
		v.Err = problem
	}
	v.MinFree = state.Format(state.MinFreeBytes)
	v.StoragePath = strings.TrimSpace(prefill)
	if v.StoragePath == "" {
		v.StoragePath = s.StoragePath()
	}
	if v.StoragePath == "" && s.StateDir != "" {
		if saved, err := state.Read(s.StateDir); err == nil {
			v.StoragePath = saved
		}
	}
	if v.StoragePath != "" {
		info, err := state.Inspect(v.StoragePath)
		if info.Total > 0 {
			v.StorageFree = state.Format(info.Free)
			v.StorageUsed = state.Format(info.Used())
			v.StorageReserved = state.Format(info.Reserved)
			v.HasReserved = info.Reserved > 0
			v.StorageTotal = state.Format(info.Total)
			v.StoragePercent = state.Percent(info.Used(), info.Total)
			n, counting, ok := s.bluegitUsage(info.Path)
			if ok {
				v.StorageBlueGit = state.Format(n)
			}
			v.StoragePartial = counting
		}
		if err != nil && v.Err == "" {
			v.Err = s.trErr(r, err)
		}
	}
	v.Cache = s.cacheInfo()
	s.attachUpdate(&v, s.langOf(r))
	s.render(w, "setup", v)
}

// RefreshHooks zet de pre-receive-hook opnieuw in elke repository die op de datadisk staat.
func (s *Server) RefreshHooks() {
	root := s.StoragePath()
	if root == "" || s.HookBin == "" || s.Store == nil {
		return
	}
	repos, err := s.Store.AllRepos(context.Background())
	if err != nil {
		return
	}
	for _, repo := range repos {
		dir := gitrepo.Dir(root, repo.OwnerPath, repo.Name)
		if _, err := os.Stat(dir); err != nil {
			continue
		}
		if err := gitrepo.InstallHook(dir, s.HookBin); err != nil && s.Log != nil {
			s.Log.Error("hook", "repo", repo.FullPath(), "err", err)
		}
		if repo.PushedAt.IsZero() {
			s.backfillPushed(repo, dir)
		}
	}
}

// backfillPushed vult pushed_at eenmalig voor repos van voor deze kolom:
// de nieuwste committijd over alle branches en tags. Een lege repo blijft leeg.
func (s *Server) backfillPushed(repo store.Repo, dir string) {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	raw := gitOne(ctx, s.Git, dir, "for-each-ref", "--sort=-committerdate", "--count=1", "--format=%(committerdate:unix)", "refs/heads", "refs/tags")
	sec, err := strconv.ParseInt(strings.TrimSpace(raw), 10, 64)
	if err != nil || sec <= 0 {
		return
	}
	_ = s.Store.TouchPushed(ctx, repo.ID, time.Unix(sec, 0))
}
