package apis

import (
	"context"
	"errors"
	"fmt"
	"log/slog"
	"net/http"
	"runtime"
	"time"

	"github.com/pocketbase/pocketbase/core"
	"github.com/pocketbase/pocketbase/tools/filesystem"
	"github.com/pocketbase/pocketbase/tools/list"
	"github.com/pocketbase/pocketbase/tools/router"
	"golang.org/x/sync/semaphore"
	"golang.org/x/sync/singleflight"
)

var imageContentTypes = []string{"image/png", "image/jpg", "image/jpeg", "image/gif"}
var defaultThumbSizes = []string{"100x100"}

// bindFileApi registers the file api endpoints and the corresponding handlers.
func bindFileApi(app core.App, rg *router.RouterGroup[*core.RequestEvent]) {
	api := fileApi{
		thumbGenSem:     semaphore.NewWeighted(int64(runtime.NumCPU() + 2)), // the value is arbitrary chosen and may change in the future
		thumbGenPending: new(singleflight.Group),
		thumbGenMaxWait: 60 * time.Second,
	}

	sub := rg.Group("/files")
	sub.POST("/token", api.fileToken).Bind(RequireAuth())
	sub.GET("/{collection}/{recordId}/{filename}", api.download).Bind(collectionPathRateLimit("", "file"))
}

type fileApi struct {
	// thumbGenSem is a semaphore to prevent too much concurrent
	// requests generating new thumbs at the same time.
	thumbGenSem *semaphore.Weighted

	// thumbGenPending represents a group of currently pending
	// thumb generation processes.
	thumbGenPending *singleflight.Group

	// thumbGenMaxWait is the maximum waiting time for starting a new
	// thumb generation process.
	thumbGenMaxWait time.Duration
}

func (api *fileApi) fileToken(e *core.RequestEvent) error {
	if e.Auth == nil {
		return e.UnauthorizedError("Missing auth context.", nil)
	}

	token, err := e.Auth.NewFileToken()
	if err != nil {
		return e.InternalServerError("Failed to generate file token", err)
	}

	event := new(core.FileTokenRequestEvent)
	event.RequestEvent = e
	event.Token = token
	event.Record = e.Auth

	return e.App.OnFileTokenRequest().Trigger(event, func(e *core.FileTokenRequestEvent) error {
		return e.JSON(http.StatusOK, map[string]string{
			"token": e.Token,
		})
	})
}

func (api *fileApi) download(e *core.RequestEvent) error {
	collection, err := e.App.FindCachedCollectionByNameOrId(e.Request.PathValue("collection"))
	if err != nil {
		return e.NotFoundError("", nil)
	}

	recordId := e.Request.PathValue("recordId")
	if recordId == "" {
		return e.NotFoundError("", nil)
	}

	record, err := e.App.FindRecordById(collection, recordId)
	if err != nil {
		return e.NotFoundError("", err)
	}

	filename := e.Request.PathValue("filename")

	fileField := record.FindFileFieldByFile(filename)
	if fileField == nil {
		return e.NotFoundError("", nil)
	}

	// check whether the request is authorized to view the protected file
	if fileField.Protected {
		originalRequestInfo, err := e.RequestInfo()
		if err != nil {
			return e.InternalServerError("Failed to load request info", err)
		}

		token := e.Request.URL.Query().Get("token")
		authRecord, _ := e.App.FindAuthRecordByToken(token, core.TokenTypeFile)

		// create a shallow copy of the cached request data and adjust it to the current auth record (if any)
		requestInfo := *originalRequestInfo
		requestInfo.Context = core.RequestInfoContextProtectedFile
		requestInfo.Auth = authRecord

		if ok, _ := e.App.CanAccessRecord(record, &requestInfo, record.Collection().ViewRule); !ok {
			return e.NotFoundError("", errors.New("insufficient permissions to access the file resource"))
		}
	}

	baseFilesPath := record.BaseFilesPath()

	// fetch the original view file field related record
	if collection.IsView() {
		fileRecord, err := e.App.FindRecordByViewFile(collection.Id, fileField.Name, filename)
		if err != nil {
			return e.NotFoundError("", fmt.Errorf("failed to fetch view file field record: %w", err))
		}
		baseFilesPath = fileRecord.BaseFilesPath()
	}

	fsys, err := e.App.NewFilesystem()
	if err != nil {
		return e.InternalServerError("Filesystem initialization failure.", err)
	}
	defer fsys.Close()

	originalPath := baseFilesPath + "/" + filename
	servedPath := originalPath
	servedName := filename

	// check for valid thumb size param
	thumbSize := e.Request.URL.Query().Get("thumb")
	if thumbSize != "" && (list.ExistInSlice(thumbSize, defaultThumbSizes) || list.ExistInSlice(thumbSize, fileField.Thumbs)) {
		// extract the original file meta attributes and check it existence
		oAttrs, oAttrsErr := fsys.Attributes(originalPath)
		if oAttrsErr != nil {
			return e.NotFoundError("", err)
		}

		// check if it is an image
		if list.ExistInSlice(oAttrs.ContentType, imageContentTypes) {
			// add thumb size as file suffix
			servedName = thumbSize + "_" + filename
			servedPath = baseFilesPath + "/thumbs_" + filename + "/" + servedName

			// create a new thumb if it doesn't exist
			if exists, _ := fsys.Exists(servedPath); !exists {
				if err := api.createThumb(e, fsys, originalPath, servedPath, thumbSize); err != nil {
					e.App.Logger().Warn(
						"Fallback to original - failed to create thumb "+servedName,
						slog.Any("error", err),
						slog.String("original", originalPath),
						slog.String("thumb", servedPath),
					)

					// fallback to the original
					servedName = filename
					servedPath = originalPath
				}
			}
		}
	}

	event := new(core.FileDownloadRequestEvent)
	event.RequestEvent = e
	event.Collection = collection
	event.Record = record
	event.FileField = fileField
	event.ServedPath = servedPath
	event.ServedName = servedName

	// clickjacking shouldn't be a concern when serving uploaded files,
	// so it safe to unset the global X-Frame-Options to allow files embedding
	// (note: it is out of the hook to allow users to customize the behavior)
	e.Response.Header().Del("X-Frame-Options")

	return e.App.OnFileDownloadRequest().Trigger(event, func(e *core.FileDownloadRequestEvent) error {
		if err := fsys.Serve(e.Response, e.Request, e.ServedPath, e.ServedName); err != nil {
			return e.NotFoundError("", err)
		}

		return nil
	})
}

func (api *fileApi) createThumb(
	e *core.RequestEvent,
	fsys *filesystem.System,
	originalPath string,
	thumbPath string,
	thumbSize string,
) error {
	ch := api.thumbGenPending.DoChan(thumbPath, func() (any, error) {
		ctx, cancel := context.WithTimeout(e.Request.Context(), api.thumbGenMaxWait)
		defer cancel()

		if err := api.thumbGenSem.Acquire(ctx, 1); err != nil {
			return nil, err
		}
		defer api.thumbGenSem.Release(1)

		return nil, fsys.CreateThumb(originalPath, thumbPath, thumbSize)
	})

	res := <-ch

	api.thumbGenPending.Forget(thumbPath)

	return res.Err
}