package service

import (
	"bpmsapi/dto"
	"bpmsapi/entity"
	"bpmsapi/repository"
)

type Quotationusecase interface {
	UpdateQuotationDetails(input entity.UpdateQuotationDetailsInput) entity.CustomResponse
}

type QuotationService struct {
	quotationRepository *repository.QuotationRepository
}

func QuotationServiceInit(quotationRepository *repository.QuotationRepository) *QuotationService {
	return &QuotationService{
		quotationRepository: quotationRepository,
	}
}

func (quotationServ *QuotationService) UpdateQuotationDetails(input entity.UpdateQuotationDetailsInput) (response entity.CustomResponse) {

	tx, errt := quotationServ.quotationRepository.Begin()
	if errt != nil {
		response.Resultcode = 1
		response.Message = errt.Error()
		return response
	}

	resp, err := quotationServ.quotationRepository.UpdateQuotation(tx, input)

	if err != nil {
		tx.Rollback()
		response.Resultcode = 1
		response.Message = err.Error()

	} else {
		commitError := tx.Commit()

		if commitError != nil {
			response.Resultcode = 1
			response.Message = dto.ErrorMessageUpdateQuotationDetails + err.Error()

		} else {
			response.Resultcode = 0
			response.Message = dto.SuccessMessageUpdateQuotationDetails
			response.Payload = resp

		}

	}

	return response
}
