package app_user

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

type deleteOpts struct {
	AppID  string `src:"path@app_id" desc:"应用ID"`
	UserID string `src:"path@user_id" desc:"用户ID"`
}

var _ = Router.Delete("/{user_id}", "删除应用用户", deleteAppUser)

type deleteAppUserResponse struct {
	Message string `json:"message"`
	AppID   string `json:"app_id"`
	UserID  string `json:"user_id"`
}

func deleteAppUser(x *vigo.X, opts *deleteOpts) (*deleteAppUserResponse, error) {
	appUser := &models.AppUser{}
	if err := cfg.DB().Where("app_id = ? AND user_id = ?", opts.AppID, opts.UserID).First(appUser).Error; err != nil {
		return nil, vigo.NewError("app_user not found").WithCode(404)
	}

	if err := cfg.DB().Delete(appUser).Error; err != nil {
		return nil, err
	}

	return &deleteAppUserResponse{
		Message: "app_user deleted successfully",
		AppID:   opts.AppID,
		UserID:  opts.UserID,
	}, nil
}
