package service

import (
	"bpmsapi/dto"
	"bpmsapi/entity"
	"bpmsapi/repository"
	"database/sql"
	"log"
)

// service methods
type Srqusecase interface {
	CreateRequest(input entity.CreateRequestInput, img_path string) entity.CustomResponse
	GetProblemDescription(input entity.GetProblemDescriptionInput) entity.CustomResponse
	FetServiceRequest(input entity.FetchServiceRequestInput) entity.CustomResponse
	UpdateProblemCategory(input entity.UpdateProblemCategoryInput) entity.CustomResponse
	UpdateServiceRequestService(User_Id int, Srq_Id int, Status string, Srq_Problem_Desc string, Srq_Visit_Timeslot string, img_paths []string) entity.CustomResponse
	UpdateServiceRequestAdminService(input entity.UpdateServiceRequestAdminInput) entity.CustomResponse
}

// repository user by srqservice
type SrqService struct {
	srqRepository     *repository.ServiceRequestRepository
	serviceRepository *repository.ServiceRepository
	adminRepository   *repository.AdminRepository
}

// initialization
func SrqServiceInit(srqRepository *repository.ServiceRequestRepository, serviceRepository *repository.ServiceRepository, adminRepository *repository.AdminRepository) *SrqService {
	return &SrqService{
		srqRepository:     srqRepository,
		serviceRepository: serviceRepository,
		adminRepository:   adminRepository,
	}

}

func (srqServ *SrqService) CreateRequest(input entity.CreateRequestInput, img_paths []string) (response entity.CustomResponse) {

	tx, errt := srqServ.srqRepository.Begin()
	if errt != nil {
		response.Resultcode = 1
		response.Message = errt.Error()
		return response

	}

	resp, err := srqServ.srqRepository.CreateServiceRequest(tx, input, img_paths)

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

	commitError := tx.Commit()

	if commitError != nil || commitError == sql.ErrNoRows {
		response.Resultcode = 1
		response.Message = dto.ErrorMessageServiceRequestCreate + err.Error()
		return response
	} else {
		response.Resultcode = 0
		response.Message = dto.SuccessMessageServiceRequestCreate
		response.Payload = resp
		return response
	}

}

func (srqServ *SrqService) GetProblemDescription(input entity.GetProblemDescriptionInput) (response entity.CustomResponse) {
	resp, err := srqServ.srqRepository.GetProblemName(input)
	if err == nil {
		if len(resp) == 0 {
			response.Resultcode = 0
			response.Message = "No problem name found"
		} else {
			response.Resultcode = 0
			response.Message = dto.SuccessMessageGetProblemDescription
			response.Payload = resp
		}
	} else {
		response.Resultcode = 1
		response.Message = dto.ErrorMessageGetProblemDescription + err.Error()
	}
	return response
}
func (srqServ *SrqService) FetchServiceRequest(input entity.FetchServiceRequestInput) (response entity.CustomResponse) {
	resp, err := srqServ.srqRepository.ServiceRequestFetch(input)

	if err != nil && err != sql.ErrNoRows {
		response.Resultcode = 1
		response.Message = dto.ErrorMessageFetchServiceRequest + err.Error()
	} else {
		if len(resp.ServiceRequests) > 0 && input.Page <= resp.Total_Page && err != sql.ErrNoRows {
			response.Resultcode = 0
			response.Message = dto.SuccessMessageFetchServiceRequest
			response.Payload = resp
		} else {
			if input.Page > resp.Total_Page && len(resp.ServiceRequests) > 0 || err == sql.ErrNoRows {
				response.Resultcode = 0
				response.Message = "No service request found for selected category"
			} else if len(resp.ServiceRequests) == 0 {
				response.Resultcode = 0
				response.Message = "No service request found for selected category"
			}

		}
	}
	return response
}

func (srqServ *SrqService) UpdateProblemCategory(input entity.UpdateProblemCategoryInput) (response entity.CustomResponse) {
	tx, errt := srqServ.serviceRepository.Begin()
	if errt != nil {
		response.Resultcode = 1
		response.Message = errt.Error()
	}

	resp, err := srqServ.serviceRepository.ProblemCategoryUpdate(tx, input)

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

	}
	commitError := tx.Commit()
	if commitError != nil {
		response.Resultcode = 1
		response.Message = dto.ErrorMessageUpdateProblemCategory + err.Error()
	} else {
		response.Resultcode = 0
		response.Message = dto.SuccessMessageUpdateProblemCategory
		response.Payload = resp
	}

	return response

}

func (srqServ *SrqService) UpdateServiceRequestService(User_Id int, Srq_Id int, Status string, Srq_Problem_Desc string, timeslot string, img_paths []string, language int, payment_mode string, payment_status string) (response entity.CustomResponse) {
	tx, errt := srqServ.srqRepository.Begin()
	if errt != nil {
		response.Resultcode = 1
		response.Message = errt.Error()
		return response

	}
	updateServiceRequestResponse, err := srqServ.srqRepository.UpdateServiceRequest(tx, User_Id, Srq_Id, Status, Srq_Problem_Desc, timeslot, img_paths, language, payment_mode, payment_status)
	if err != nil {
		tx.Rollback()
		response.Resultcode = 1
		response.Message = dto.ErrorMessageUpdateServiceRequest + err.Error()
		return response
	}

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

func (srqServ *SrqService) UpdateServiceRequestAdminService(input entity.UpdateServiceRequestAdminInput) (response entity.CustomResponse) {
	tx, errt := srqServ.adminRepository.Begin()
	if errt != nil {
		response.Resultcode = 1
		response.Message = errt.Error()
		log.Println("AdminService: SignupAdminService: Input: ", input, "Error ", errt.Error())
		return response
	}
	updateSrqAdminResponse, err := srqServ.adminRepository.UpdateServiceRequestAdmin(tx, input)
	if err != nil {
		tx.Rollback()
		response.Resultcode = 1
		response.Message = dto.ErrorMessageUpdateSrqAdmin + err.Error()
		return response
	}

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