package service

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

// service methods
type Technicianusecase interface {
	FetchTechnicianService(Service_Id int, Apartment_Id int, Time_slot string) entity.CustomResponse
	GetTechnicianListService(input entity.GetTechnicianListInput) entity.CustomResponse
	UpdateServiceRequestTech(input entity.UpdateSrqTechInput, image_path string) entity.CustomResponse
	GetConfirmationPin(input entity.GetConfirmationPinInput) entity.CustomResponse
	GetRole(input entity.GetRoleInput) entity.CustomResponse
	GetAllEmployee(input entity.GetAllEmployeeInput) entity.CustomResponse
}

// repository user by srqservice
type TechnicianService struct {
	technicianRepository *repository.TechnicianRepository
}

// initialization
func TechnicianServiceInit(technicianRepository *repository.TechnicianRepository) *TechnicianService {
	return &TechnicianService{
		technicianRepository: technicianRepository,
	}

}

// methods, call to repository db
func (technicianServ *TechnicianService) FetchTechnicianService(input entity.FetchTechnicianInput) (response entity.CustomResponse) {
	fetchTechnicianResponse, err := technicianServ.technicianRepository.FetchTechnician(input)
	if err == nil {
		if len(fetchTechnicianResponse.TechList) == 0 || input.Page > fetchTechnicianResponse.Page {
			response.Resultcode = 0
			response.Message = "No technician is found for this category"

		} else {
			response.Resultcode = 0
			response.Message = dto.SuccessMessageTechniciansFetch
			response.Payload = fetchTechnicianResponse
		}

	} else {
		response.Resultcode = 1
		response.Message = dto.ErrorMessageTechniciansFetch + err.Error()
	}
	return response
}

func (technicianServ *TechnicianService) GetTechnicianListService(input entity.GetTechnicianListInput) (response entity.CustomResponse) {
	resp, err := technicianServ.technicianRepository.FetchTechnicianList(input)
	if err == nil {
		if len(resp.TechniciansLists) == 0 || input.Page > resp.Page {
			response.Resultcode = 0
			response.Message = "No technician is found for this category"
			return response
		} else {
			response.Resultcode = 0
			response.Message = dto.SuccessMessageAdminsFetch
			response.Payload = resp
		}
	} else {
		response.Resultcode = 1
		response.Message = dto.ErrorMessageFetchTechniciansList + err.Error()
	}

	return response
}
func (technicianServ *TechnicianService) UpdateServiceRequestTech(input entity.UpdateSrqTechInput, image_path string) (response entity.CustomResponse) {
	tx, errt := technicianServ.technicianRepository.Begin()
	if errt != nil {
		response.Resultcode = 1
		response.Message = errt.Error()
	}

	resp, err := technicianServ.technicianRepository.TechUpdateServiceRequest(tx, input, image_path)

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

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

	return response

}

func (technicianServ *TechnicianService) GetConfirmationPin(input entity.GetConfirmationPinInput) (response entity.CustomResponse) {
	tx, errt := technicianServ.technicianRepository.Begin()

	if errt != nil {
		response.Resultcode = 1
		response.Message = errt.Error()
	}

	resp, err := technicianServ.technicianRepository.GetPinConfirmation(tx, input)

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

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

	return response
}

func (technicianServ *TechnicianService) GetRole(input entity.GetRoleInput) (response entity.CustomResponse) {
	resp, err := technicianServ.technicianRepository.FetchRole(input)
	if err != nil && err != sql.ErrNoRows {

		response.Resultcode = 1
		response.Message = dto.ErrorMessageGetRole + err.Error()
	} else {
		if err == sql.ErrNoRows {
			response.Resultcode = 0
			response.Message = "Mentioned mobile number is not mapped as any management users. Please reach out to the admin"
		} else {
			response.Resultcode = 0
			response.Message = dto.SuccessMessageGetRole
			response.Payload = resp
		}

	}
	return response
}

func (technicianServ *TechnicianService) GetAllEmployee(input entity.GetAllEmployeeInput) (response entity.CustomResponse) {
	resp, err := technicianServ.technicianRepository.FetchAllEmployee(input)
	if err != nil {
		response.Resultcode = 1
		response.Message = dto.ErrorMessageGetAllEmployee + err.Error()
	} else {

		if len(resp.EmployeeList) == 0 || input.Page > resp.Page {
			response.Resultcode = 0
			response.Message = "No employee is found for this category"
		} else {
			response.Resultcode = 0
			response.Message = dto.SuccessMessageGetAllEmployee
			response.Payload = resp
		}
	}
	return response
}
