/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 *
 * This work is published and licensed under a Creative Commons
 * Attribution-NonCommercial-NoDerivatives 4.0 International License.
 *
 * Distribution of this work is permitted, but commercial use and
 * modifications are strictly prohibited.
 */
package handler

import (
	"fmt"

	"mstore/app/operator"
	"mstore/app/router"
	"mstore/pkg/terms"
)

// POST /v3/grant/create 200 200
func (hand *Handler) CreateGrant(rctx *router.Context) {
	var err error

	params := &operator.CreateGrantParams{}
	err = rctx.BindJSON(params)
	if err != nil {
		hand.SendError(rctx, err)
		return
	}
	// Rigth checking
	operatorID, _ := rctx.GetString(userTag)
	opEnable, err := hand.CheckRight(rctx.Ctx, operatorID, terms.RightWriteAccounts, "")
	if err != nil {
		err := fmt.Errorf("Operation error: %v", err)
		hand.SendError(rctx, err)
		return
	}
	if !opEnable {
		err := fmt.Errorf("Operation not enabled for this account")
		hand.SendError(rctx, err)
		return
	}
	// Execution of the operation
	res, err := hand.oper.CreateGrant(rctx.Ctx, operatorID, params)
	if err != nil {
		hand.logg.Errorf("CreateGrant error: %v", err)
		hand.SendError(rctx, err)
		return
	}
	hand.SendResult(rctx, res)
}

// POST /v3/grant/get 200 200
func (hand *Handler) GetGrant(rctx *router.Context) {
	var err error

	params := &operator.GetGrantParams{}
	err = rctx.BindJSON(params)
	if err != nil {
		hand.SendError(rctx, err)
		return
	}
	// Rigth checking
	operatorID, _ := rctx.GetString(userTag)
	opEnable, err := hand.CheckRight(rctx.Ctx, operatorID, terms.RightReadAccounts, "")
	if err != nil {
		err := fmt.Errorf("Operation error: %v", err)
		hand.SendError(rctx, err)
		return
	}
	if !opEnable {
		err := fmt.Errorf("Operation not enabled for this account")
		hand.SendError(rctx, err)
		return
	}
	// Execution of the operation
	res, err := hand.oper.GetGrant(rctx.Ctx, operatorID, params)
	if err != nil {
		hand.logg.Errorf("CreateGrant error: %v", err)
		hand.SendError(rctx, err)
		return
	}
	hand.SendResult(rctx, res)
}

// POST /v3/grants/list 200 200
func (hand *Handler) ListGrants(rctx *router.Context) {
	var err error

	params := &operator.ListGrantsParams{}
	err = rctx.BindJSON(params)
	if err != nil {
		hand.SendError(rctx, err)
		return
	}
	// Rigth checking
	operatorID, _ := rctx.GetString(userTag)
	opEnable, err := hand.CheckRight(rctx.Ctx, operatorID, terms.RightReadAccounts, "")
	if err != nil {
		err := fmt.Errorf("Operation error: %v", err)
		hand.SendError(rctx, err)
		return
	}
	if !opEnable {
		err := fmt.Errorf("Operation not enabled for this account")
		hand.SendError(rctx, err)
		return
	}
	// Execution of the operation
	res, err := hand.oper.ListGrants(rctx.Ctx, operatorID, params)
	if err != nil {
		hand.logg.Errorf("ListGrants error: %v", err)
		hand.SendError(rctx, err)
		return
	}
	hand.SendResult(rctx, res)
}

// POST /v3/grant/get 200 200
func (hand *Handler) UpdateGrant(rctx *router.Context) {
	var err error

	params := &operator.UpdateGrantParams{}
	err = rctx.BindJSON(params)
	if err != nil {
		hand.SendError(rctx, err)
		return
	}
	// Rigth checking
	operatorID, _ := rctx.GetString(userTag)
	opEnable, err := hand.CheckRight(rctx.Ctx, operatorID, terms.RightWriteAccounts, "")
	if err != nil {
		err := fmt.Errorf("Operation error: %v", err)
		hand.SendError(rctx, err)
		return
	}
	if !opEnable {
		err := fmt.Errorf("Operation not enabled for this account")
		hand.SendError(rctx, err)
		return
	}
	// Execution of the operation
	res, err := hand.oper.UpdateGrant(rctx.Ctx, operatorID, params)
	if err != nil {
		hand.logg.Errorf("UpdateGrant error: %v", err)
		hand.SendError(rctx, err)
		return
	}
	hand.SendResult(rctx, res)
}

// POST /v3/grant/delete 200 200
func (hand *Handler) DeleteGrant(rctx *router.Context) {
	var err error

	params := &operator.DeleteGrantParams{}
	err = rctx.BindJSON(params)
	if err != nil {
		hand.SendError(rctx, err)
		return
	}
	// Rigth checking
	operatorID, _ := rctx.GetString(userTag)
	opEnable, err := hand.CheckRight(rctx.Ctx, operatorID, terms.RightWriteAccounts, "")
	if err != nil {
		err := fmt.Errorf("Operation error: %v", err)
		hand.SendError(rctx, err)
		return
	}
	if !opEnable {
		err := fmt.Errorf("Operation not enabled for this account")
		hand.SendError(rctx, err)
		return
	}
	// Execution of the operation
	res, err := hand.oper.DeleteGrant(rctx.Ctx, operatorID, params)
	if err != nil {
		hand.logg.Errorf("DeleteGrant error: %v", err)
		hand.SendError(rctx, err)
		return
	}
	hand.SendResult(rctx, res)
}
