package service

import (
	"bpmsapi/dto"
	"bpmsapi/entity"
	"bpmsapi/repository"
	"database/sql"
	// "github.com/jmoiron/sqlx"
)

// service methods
type Srvusecase interface {
	AddSrvService(input entity.AddServiceInput, img_path1 string, img_path2 string) (response entity.CustomResponse)
}

type SrvService struct {
	srvRepository *repository.ServiceRepository
}

// initialization
func SrvServiceInit(srvRepository *repository.ServiceRepository) *SrvService {
	return &SrvService{
		srvRepository: srvRepository,
	}
}

func (srvServ *SrvService) AddSrvService(input entity.AddServiceInput,img_path1 string,img_path2 string) (response entity.CustomResponse) {

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

	resp, err := srvServ.srvRepository.AddService(tx, input, img_path1,img_path2)
	if err != nil {
		tx.Rollback()
		response.Resultcode = 1
		response.Message = dto.ErrorMessageAddService + err.Error()
		return response
	}

	commitError := tx.Commit()
	if commitError != nil || commitError == sql.ErrNoRows {
		response.Resultcode = 1
		response.Message = dto.ErrorMessageAddService + commitError.Error()
		return response
	} else {
		response.Resultcode = 0
		response.Message = dto.SuccessMessageAddService
		response.Payload = resp
		return response
	}
}