package service

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

	"github.com/jmoiron/sqlx"
)

type Profileusecase interface {
	UpdateEmployeeProfile(input entity.UpdateEmployeeProfileInput, img_path string) entity.CustomResponse
	EmpApartmentConfig(input entity.EmpApartmentConfigInput) entity.CustomResponse
	CreateLeave(input entity.CreateLeaveInput) entity.CustomResponse
	CancelLeave(input entity.CancelLeaveInput) entity.CustomResponse
	DeleteEmployee(input entity.DeleteEmployeeInput)  entity.CustomResponse
	
}

type ProfileService struct {
	profileRepository *repository.ProfileRepository
	tx                *sqlx.Tx
}

func ProfileServiceInit(profileRepository *repository.ProfileRepository, tx *sqlx.Tx) *ProfileService {
	return &ProfileService{
		profileRepository: profileRepository,
		tx:                tx,
	}
}

func (profileServ *ProfileService) UpdateEmployeeProfile(input entity.UpdateEmployeeProfileInput, img_path string) (response entity.CustomResponse) {
	resp, err := profileServ.profileRepository.EmployeeProfileUpdate(input, img_path)
	if err != nil {
		profileServ.tx.Rollback()
		response.Resultcode = 1
		response.Message = err.Error()
	} else {
		commitError := profileServ.tx.Commit()
		if commitError != nil {
			response.Resultcode = 1
			response.Message = dto.ErrorMessageEmployeeProfile + err.Error()
		} else {
			if len(resp.EmployeeProfile) == 0 {
				response.Resultcode = 0
				response.Message = dto.ErrorMessageEmployeeProfile + " No values updated in the db"

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

	}

	return response
}

func (profileServ *ProfileService) EmpApartmentConfig(input entity.EmpApartmentConfigInput) (response entity.CustomResponse) {
	resp, err := profileServ.profileRepository.EmployeeApartmentConfig(input)

	if err != nil {
		profileServ.tx.Rollback()
		response.Resultcode = 1
		response.Message = resp + err.Error()
	} else {
		commitError := profileServ.tx.Commit()
		if commitError != nil {
			response.Resultcode = 1
			response.Message = resp
		} else {
			response.Resultcode = 0
			response.Message = resp
		}
	}
	return response
}

func (profileServ *ProfileService) CreateLeave(input entity.CreateLeaveInput) (response entity.CustomResponse) {
	resp, err := profileServ.profileRepository.EmployeeCreateLeave(input)

	if err != nil {
		if strings.Contains(err.Error(), "Error 1292 (22007): Incorrect datetime value") {
			profileServ.tx.Rollback()
			response.Resultcode = 1
			response.Message = "Invalid date format. Please provide a valid date."
		} else {
			profileServ.tx.Rollback()
			response.Resultcode = 1
			response.Message = resp + err.Error()
		}
	} else {
		commitError := profileServ.tx.Commit()
		if commitError != nil {
			response.Resultcode = 1
			response.Message = resp + err.Error()
		} else {
			response.Resultcode = 0
			response.Message = resp
		}
	}
	return response

}


func (profileServ *ProfileService) CancelLeave(input entity.CancelLeaveInput) (response entity.CustomResponse) {
	resp, err := profileServ.profileRepository.EmployeeCancelLeave(input)

	if err != nil {
			profileServ.tx.Rollback()
			response.Resultcode = 1
			response.Message = resp + err.Error()
	} else {
		commitError := profileServ.tx.Commit()
		if commitError != nil {
			response.Resultcode = 1
			response.Message = resp + err.Error()
		} else {
			response.Resultcode = 0
			response.Message = resp
		}
	}
	return response

}


func (profileServ *ProfileService) DeleteEmployee(input entity.DeleteEmployeeInput) (response entity.CustomResponse) {
	resp, err := profileServ.profileRepository.DeleteEmployeeAccount(input)

	if err != nil {
			profileServ.tx.Rollback()
			response.Resultcode = 1
			response.Message = resp + err.Error()
	} else {
		commitError := profileServ.tx.Commit()
		if commitError != nil {
			response.Resultcode = 1
			response.Message = resp + err.Error()
		} else {
			response.Resultcode = 0
			response.Message = resp
		}
	}
	return response

}
