package fs

import (
	"codeberg.org/shroff/phylum/server/internal/api/authenticator"
	"codeberg.org/shroff/phylum/server/internal/api/v1/responses"
	"github.com/gin-gonic/gin"
)

type deleteParams struct {
	Path string `json:"path" form:"path" binding:"required"`
	Hard bool   `json:"permanent" form:"permanent"`
}

func handleDeleteRequest(c *gin.Context) {
	var params deleteParams
	err := c.ShouldBind(&params)
	if err != nil {
		err = c.BindQuery(&params)
		if err != nil {
			panic(errInvalidParams)
		}
	}

	fs := authenticator.GetFileSystem(c)
	r, err := fs.ResourceByPathWithRoot(params.Path)
	if err != nil {
		panic(err)
	}
	if params.Hard {
		if err := fs.DeleteForever(r); err != nil {
			panic(err)
		} else {
			c.JSON(200, gin.H{"deleted": r.ID().String()})
		}
	} else {
		if r, err := fs.Delete(r); err != nil {
			panic(err)
		} else {
			c.JSON(200, responses.ResourceFromFS(r))
		}
	}
}
