/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 */
package handler

import (
	"fmt"

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

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

	params := &accoper.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.acop.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 := &accoper.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.acop.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 := &accoper.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.acop.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 := &accoper.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.acop.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 := &accoper.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.acop.DeleteGrant(rctx.Ctx, operatorID, params)
	if err != nil {
		hand.logg.Errorf("DeleteGrant error: %v", err)
		hand.SendError(rctx, err)
		return
	}
	hand.SendResult(rctx, res)
}
