package resource

import (
	"github.com/veypi/vbase/cfg"
	"github.com/veypi/vbase/models"
	"github.com/veypi/vigo"
)

type deleteOpts struct {
	ResourceID string `src:"path@resource_id" desc:"资源ID"`
}

var _ = Router.Delete("/{resource_id}", "删除资源", deleteResource)

type deleteResourceResponse struct {
	Message string `json:"message"`
	ID      string `json:"id"`
}

func deleteResource(x *vigo.X, opts *deleteOpts) (*deleteResourceResponse, error) {
	// 查找资源
	resource := &models.Resource{}
	if err := cfg.DB().Where("id = ?", opts.ResourceID).First(resource).Error; err != nil {
		return nil, vigo.NewError("resource not found").WithCode(404)
	}

	// 删除资源
	if err := cfg.DB().Delete(resource).Error; err != nil {
		return nil, vigo.NewError("failed to delete resource").WithCode(500)
	}

	return &deleteResourceResponse{
		Message: "resource deleted successfully",
		ID:      opts.ResourceID,
	}, nil
}
