// Copyright 2025 Daytona Platforms Inc.
// SPDX-License-Identifier: AGPL-3.0

package fs

import (
	"errors"
	"fmt"
	"net/http"
	"os"
	"path/filepath"

	"github.com/gin-gonic/gin"
)

// DownloadFile godoc
//
//	@Summary		Download a file
//	@Description	Download a file by providing its path
//	@Tags			file-system
//	@Produce		octet-stream
//	@Param			path	query	string	true	"File path to download"
//	@Success		200		{file}	binary
//	@Router			/files/download [get]
//
//	@id				DownloadFile
func DownloadFile(c *gin.Context) {
	requestedPath := c.Query("path")
	if requestedPath == "" {
		c.AbortWithError(http.StatusBadRequest, errors.New("path is required"))
		return
	}

	absPath, err := filepath.Abs(requestedPath)
	if err != nil {
		c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid path: %w", err))
		return
	}

	fileInfo, err := os.Stat(absPath)
	if err != nil {
		if os.IsNotExist(err) {
			c.AbortWithError(http.StatusNotFound, err)
			return
		}
		if os.IsPermission(err) {
			c.AbortWithError(http.StatusForbidden, err)
			return
		}
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	if fileInfo.IsDir() {
		c.AbortWithError(http.StatusBadRequest, errors.New("path must be a file"))
		return
	}

	c.Header("Content-Description", "File Transfer")
	c.Header("Content-Type", "application/octet-stream")
	filename := filepath.Base(absPath)
	c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"; filename*=utf-8''%s`,
		toLatin1(filename), encodeRFC5987(filename)))
	c.Header("Content-Transfer-Encoding", "binary")
	c.Header("Expires", "0")
	c.Header("Cache-Control", "must-revalidate")
	c.Header("Pragma", "public")

	c.File(absPath)
}
