package order

import (
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
	"git.gocasts.ir/ebhomengo/niki/purchaseapp/entity"
	"git.gocasts.ir/ebhomengo/niki/types"
)

type Service struct {
	repo Repo
}

type Repo interface {
	CreateOrder(order entity.Order, orderItems []entity.OrderItem) (types.ID, error)
	UpdateOrderProcessStatus(orderID types.ID, status string) (bool, error)
}

func New(orderRepo Repo) Service {
	return Service{repo: orderRepo}
}

func (s Service) CreateOrder(order entity.Order, orderItems []entity.OrderItem) (CreateOrderResponse, error) {
	const Op = "purchaseapp.service.CreateOrder"
	orderID, err := s.repo.CreateOrder(order, orderItems)

	if err != nil {
		return CreateOrderResponse{}, richerror.New(Op).WithErr(err)
	}

	return CreateOrderResponse{OrderID: orderID}, nil
}

func (s Service) UpdateOrderProcessStatus(orderID types.ID, status string) (bool, error) {

	const Op = "purchaseapp.service.UpdateOrderProcessStatus"
	_, err := s.repo.UpdateOrderProcessStatus(orderID, status)
	if err != nil {
		return false, richerror.New(Op).WithErr(err)
	}

	return true, nil
}
