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

import (
	"fmt"

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

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

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

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

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

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

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

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

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

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

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