package service

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

	"github.com/jmoiron/sqlx"
)

type Apartmentusecase interface {
	AddNewApartment(input entity.AddApartmentInput, apartment_img string) entity.CustomResponse
	GetApartment(input entity.GetApartmentDetailsInput) entity.CustomResponse
	UpdateApartment(input entity.UpdateApartmentInput) entity.CustomResponse
	ApartmentUserMapping(input entity.UserApartmentMappingInput) entity.CustomResponse
	ApartmentUserUpdate(input entity.UserApartmentUpdateInput) entity.CustomResponse
}

type ApartmentService struct {
	apartmentRepository *repository.ApartmentRepository
	tx                  *sqlx.Tx
}

func ApartmentServiceInit(apartmentRepository *repository.ApartmentRepository, tx *sqlx.Tx) *ApartmentService {
	return &ApartmentService{
		apartmentRepository: apartmentRepository,
		tx:                  tx,
	}
}

func (apartmentServ *ApartmentService) AddNewApartment(input entity.AddApartmentInput, apartment_img string) (response entity.CustomResponse) {

	resp, err := apartmentServ.apartmentRepository.AddApartment(input, apartment_img)
	if err != nil {
		apartmentServ.tx.Rollback()
		response.Resultcode = 1
		response.Message = dto.ErrorMessageAddApartment + err.Error()
	}
	commitError := apartmentServ.tx.Commit()
	if commitError != nil {
		response.Resultcode = 1
		response.Message = dto.ErrorMessageAddApartment + err.Error()

	} else {
		if len(resp) == 0 {
			response.Resultcode = 1
			response.Message = dto.ErrorMessageAddApartment + " No values changed in the db"
		} else {
			response.Resultcode = 0
			response.Message = dto.SuccessMessageAddApartment
			response.Payload = resp

		}
	}
	return response
}

func (apartmentServ *ApartmentService) GetApartment(input entity.GetApartmentDetailsInput) (response entity.CustomResponse) {
	resp, err := apartmentServ.apartmentRepository.FetchApartmentDetails(input)
	if err != nil {
		apartmentServ.tx.Rollback()
		response.Resultcode = 1
		response.Message = err.Error()
	}

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

	return response

}
func (apartmentServ *ApartmentService) UpdateApartment(input entity.UpdateApartmentInput, apartment_img string, banner_imgs []string) (response entity.CustomResponse) {

	resp, err := apartmentServ.apartmentRepository.UpdateApartmentDetails(input, apartment_img, banner_imgs)
	if err != nil {
		apartmentServ.tx.Rollback()
		response.Resultcode = 1
		response.Message = resp + err.Error()
	} else {
		commitError := apartmentServ.tx.Commit()
		if commitError != nil {
			response.Resultcode = 1
			response.Message = resp + err.Error()
		} else {
			response.Resultcode = 0
			response.Message = resp
		}
	}

	return response
}

func (apartmentServ *ApartmentService) ApartmentUserMapping(input entity.UserApartmentMappingInput) (response entity.CustomResponse) {

	resp, err := apartmentServ.apartmentRepository.UserApartmentMapping(input)
	if err != nil {
		apartmentServ.tx.Rollback()
		response.Resultcode = 1
		response.Message = dto.ErroruserApartmentMapping + err.Error()
	} else {
		commitError := apartmentServ.tx.Commit()
		if commitError != nil {
			response.Resultcode = 1
			response.Message = dto.ErroruserApartmentMapping + err.Error()
		} else {
			response.Resultcode = 0
			response.Message = dto.SuccessuserApartmentMapping
			response.Payload = resp
		}
	}

	return response
}

func (apartmentServ *ApartmentService) UpdateUserApartment(input entity.UserApartmentUpdateInput) (response entity.CustomResponse) {

	resp, err := apartmentServ.apartmentRepository.UpdateUserApartmentInfo(input)
	if err != nil {
		apartmentServ.tx.Rollback()
		response.Resultcode = 1
		response.Message = dto.ErroruserApartmentUpdate + err.Error()
	} else {
		commitError := apartmentServ.tx.Commit()
		if commitError != nil {
			response.Resultcode = 1
			response.Message = dto.ErroruserApartmentUpdate + err.Error()
		} else {
			response.Resultcode = 0
			response.Message = dto.SuccessuserApartmentUpdate
			response.Payload = resp
		}
	}

	return response
}
