package server

import (
	"fmt"
	"net/http"
	"path"
	"strings"

	"github.com/rs/zerolog/log"

	"codeberg.org/codeberg/pages/html"
	"codeberg.org/codeberg/pages/server/cache"
	"codeberg.org/codeberg/pages/server/context"
	"codeberg.org/codeberg/pages/server/dns"
	"codeberg.org/codeberg/pages/server/gitea"
	"codeberg.org/codeberg/pages/server/upstream"
	"codeberg.org/codeberg/pages/server/utils"
	"codeberg.org/codeberg/pages/server/version"
)

const (
	headerAccessControlAllowOrigin  = "Access-Control-Allow-Origin"
	headerAccessControlAllowMethods = "Access-Control-Allow-Methods"
)

// Handler handles a single HTTP request to the web server.
func Handler(mainDomainSuffix, rawDomain string,
	giteaClient *gitea.Client,
	rawInfoPage string,
	blacklistedPaths, allowedCorsDomains []string,
	dnsLookupCache, canonicalDomainCache cache.SetGetKey,
) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		log := log.With().Strs("Handler", []string{string(req.Host), req.RequestURI}).Logger()
		ctx := context.New(w, req)

		ctx.RespWriter.Header().Set("Server", "CodebergPages/"+version.Version)

		// Force new default from specification (since November 2020) - see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy#strict-origin-when-cross-origin
		ctx.RespWriter.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")

		// Enable browser caching for up to 10 minutes
		ctx.RespWriter.Header().Set("Cache-Control", "public, max-age=600")

		trimmedHost := utils.TrimHostPort(req.Host)

		// Add HSTS for RawDomain and MainDomainSuffix
		if hsts := getHSTSHeader(trimmedHost, mainDomainSuffix, rawDomain); hsts != "" {
			ctx.RespWriter.Header().Set("Strict-Transport-Security", hsts)
		}

		// Block all methods not required for static pages
		if !ctx.IsMethod(http.MethodGet) && !ctx.IsMethod(http.MethodHead) && !ctx.IsMethod(http.MethodOptions) {
			ctx.RespWriter.Header().Set("Allow", http.MethodGet+", "+http.MethodHead+", "+http.MethodOptions) // duplic 1
			ctx.String("Method not allowed", http.StatusMethodNotAllowed)
			return
		}

		// Block blacklisted paths (like ACME challenges)
		for _, blacklistedPath := range blacklistedPaths {
			if strings.HasPrefix(ctx.Path(), blacklistedPath) {
				html.ReturnErrorPage(ctx, "requested blacklisted path", http.StatusForbidden)
				return
			}
		}

		// Allow CORS for specified domains
		allowCors := false
		for _, allowedCorsDomain := range allowedCorsDomains {
			if strings.EqualFold(trimmedHost, allowedCorsDomain) {
				allowCors = true
				break
			}
		}
		if allowCors {
			ctx.RespWriter.Header().Set(headerAccessControlAllowOrigin, "*")
			ctx.RespWriter.Header().Set(headerAccessControlAllowMethods, http.MethodGet+", "+http.MethodHead)
		}

		ctx.RespWriter.Header().Set("Allow", http.MethodGet+", "+http.MethodHead+", "+http.MethodOptions) // duplic 1
		if ctx.IsMethod(http.MethodOptions) {
			ctx.RespWriter.WriteHeader(http.StatusNoContent)
			return
		}

		// Prepare request information to Gitea
		targetOptions := &upstream.Options{
			TryIndexPages: true,
		}

		log.Debug().Msg("preparations")
		if rawDomain != "" && strings.EqualFold(trimmedHost, rawDomain) {
			// Serve raw content from RawDomain
			log.Debug().Msg("raw domain")

			targetOptions.TryIndexPages = false
			targetOptions.ServeRaw = true

			pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
			if len(pathElements) < 2 {
				// https://{RawDomain}/{owner}/{repo}[/@{branch}]/{path} is required
				ctx.Redirect(rawInfoPage, http.StatusTemporaryRedirect)
				return
			}
			targetOwner := pathElements[0]
			targetRepo := pathElements[1]

			// raw.codeberg.org/example/myrepo/@main/index.html
			if len(pathElements) > 2 && strings.HasPrefix(pathElements[2], "@") {
				log.Debug().Msg("raw domain preparations, now trying with specified branch")
				newPath := path.Join(pathElements[3:]...)
				branch := pathElements[2][1:]
				if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, targetRepo, branch, newPath, true); works {
					targetOptions.BranchTimestamp = timestampBranch.Timestamp
					log.Debug().Msg("tryBranch, now trying upstream 1")
					tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
						targetOptions, targetOwner, targetRepo, timestampBranch.Branch, newPath, canonicalDomainCache)
					return
				}
				log.Debug().Msg("missing branch info")
				html.ReturnErrorPage(ctx, "missing branch info", http.StatusFailedDependency)
				return
			}

			log.Debug().Msg("raw domain preparations, now trying with default branch")
			newPath := path.Join(pathElements[2:]...)
			if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, targetRepo, "", newPath, true); works {
				targetOptions.BranchTimestamp = timestampBranch.Timestamp
				log.Debug().Msg("tryBranch, now trying upstream 2")
				tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
					targetOptions, targetOwner, targetRepo, timestampBranch.Branch, newPath, canonicalDomainCache)
			} else {
				log.Error().Msg("TODO: is this a bug?")
			}
			return

		} else if strings.HasSuffix(trimmedHost, mainDomainSuffix) {
			// Serve pages from subdomains of MainDomainSuffix
			log.Debug().Msg("main domain suffix")

			pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
			targetOwner := strings.TrimSuffix(trimmedHost, mainDomainSuffix)
			targetRepo := pathElements[0]

			if targetOwner == "www" {
				// www.codeberg.page redirects to codeberg.page // TODO: rm hardcoded - use cname?
				ctx.Redirect("https://"+string(mainDomainSuffix[1:])+string(ctx.Path()), http.StatusPermanentRedirect)
				return
			}

			// Check if the first directory is a repo with the second directory as a branch
			// example.codeberg.page/myrepo/@main/index.html
			if len(pathElements) > 1 && strings.HasPrefix(pathElements[1], "@") {
				if targetRepo == "pages" {
					// example.codeberg.org/pages/@... redirects to example.codeberg.org/@...
					ctx.Redirect("/"+strings.Join(pathElements[1:], "/"), http.StatusTemporaryRedirect)
					return
				}

				log.Debug().Msg("main domain preparations, now trying with specified repo & branch")
				branch := pathElements[1][1:]
				newPath := path.Join(pathElements[2:]...)
				repo := pathElements[0]
				if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, repo, branch, newPath, true); works {
					targetOptions.BranchTimestamp = timestampBranch.Timestamp
					log.Debug().Msg("tryBranch, now trying upstream 3")
					tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
						targetOptions, targetOwner, repo, timestampBranch.Branch, newPath, canonicalDomainCache)
				} else {
					html.ReturnErrorPage(ctx,
						fmt.Sprintf("explizite set branch %q do not exist at '%s/%s'", branch, targetOwner, targetRepo),
						http.StatusFailedDependency)
				}
				return
			}

			// Check if the first directory is a branch for the "pages" repo
			// example.codeberg.page/@main/index.html
			if strings.HasPrefix(pathElements[0], "@") {
				log.Debug().Msg("main domain preparations, now trying with specified branch")
				branch := pathElements[0][1:]
				repo := "pages"
				newPath := path.Join(pathElements[1:]...)
				if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, targetRepo, branch, newPath, true); works {
					targetOptions.BranchTimestamp = timestampBranch.Timestamp
					log.Debug().Msg("tryBranch, now trying upstream 4")
					tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
						targetOptions, targetOwner, repo, timestampBranch.Branch, newPath, canonicalDomainCache)
				} else {
					html.ReturnErrorPage(ctx,
						fmt.Sprintf("explizite set branch %q do not exist at '%s/%s'", branch, targetOwner, "pages"),
						http.StatusFailedDependency)
				}
				return
			}

			// Check if the first directory is a repo with a "pages" branch
			// example.codeberg.page/myrepo/index.html
			// example.codeberg.page/pages/... is not allowed here.
			log.Debug().Msg("main domain preparations, now trying with specified repo")
			if pathElements[0] != "pages" {
				repo := pathElements[0]
				branch := "pages"
				newPath := path.Join(pathElements[1:]...)
				if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, repo, branch, newPath, false); works {
					targetOptions.BranchTimestamp = timestampBranch.Timestamp
					log.Debug().Msg("tryBranch, now trying upstream 5")
					tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
						targetOptions, targetOwner, repo, timestampBranch.Branch, newPath, canonicalDomainCache)
					return
				}
			}

			// Try to use the "pages" repo on its default branch
			// example.codeberg.page/index.html
			log.Debug().Msg("main domain preparations, now trying with default repo/branch")
			newPath := path.Join(pathElements...)
			repo := "pages"
			if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, repo, "", newPath, false); works {
				targetOptions.BranchTimestamp = timestampBranch.Timestamp
				log.Debug().Msg("tryBranch, now trying upstream 6")
				tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
					targetOptions, targetOwner, repo, timestampBranch.Branch, newPath, canonicalDomainCache)
				return
			}

			// Couldn't find a valid repo/branch
			html.ReturnErrorPage(ctx,
				fmt.Sprintf("couldn't find a valid repo[%s]", targetRepo),
				http.StatusFailedDependency)
			return
		} else {
			trimmedHostStr := string(trimmedHost)

			// Serve pages from custom domains
			targetOwner, targetRepo, targetBranch := dns.GetTargetFromDNS(trimmedHostStr, string(mainDomainSuffix), dnsLookupCache)
			if targetOwner == "" {
				html.ReturnErrorPage(ctx,
					"could not obtain repo owner from custom domain",
					http.StatusFailedDependency)
				return
			}

			pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
			canonicalLink := false
			if strings.HasPrefix(pathElements[0], "@") {
				targetBranch = pathElements[0][1:]
				pathElements = pathElements[1:]
				canonicalLink = true
			}

			// Try to use the given repo on the given branch or the default branch
			newPath := path.Join(pathElements...)
			log.Debug().Msg("custom domain preparations, now trying with details from DNS")
			if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, targetRepo, targetBranch, newPath, canonicalLink); works {
				targetOptions.BranchTimestamp = timestampBranch.Timestamp
				canonicalDomain, valid := upstream.CheckCanonicalDomain(giteaClient, targetOwner, targetRepo, timestampBranch.Branch, trimmedHostStr, string(mainDomainSuffix), canonicalDomainCache)
				if !valid {
					html.ReturnErrorPage(ctx, "domain not specified in <code>.domains</code> file", http.StatusMisdirectedRequest)
					return
				} else if canonicalDomain != trimmedHostStr {
					// only redirect if the target is also a codeberg page!
					targetOwner, _, _ = dns.GetTargetFromDNS(strings.SplitN(canonicalDomain, "/", 2)[0], string(mainDomainSuffix), dnsLookupCache)
					if targetOwner != "" {
						ctx.Redirect("https://"+canonicalDomain+string(newPath), http.StatusTemporaryRedirect)
						return
					}

					html.ReturnErrorPage(ctx, "target is no codeberg page", http.StatusFailedDependency)
					return
				}

				log.Debug().Msg("tryBranch, now trying upstream 7")
				tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
					targetOptions, targetOwner, targetRepo, timestampBranch.Branch, newPath, canonicalDomainCache)
				return
			}

			html.ReturnErrorPage(ctx, "could not find target for custom domain", http.StatusFailedDependency)
			return
		}
	}
}