package service

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

	//"fmt"
	"strings"
)

type AdminUsecase interface {
	ValidateCustomerService(admin_id int, user_mobnum int64, account_status string, reject_reason string) entity.CustomResponse
	SignupAdminService(input entity.SignUpAdminInput, img_path string) entity.CustomResponse
	GetAdminListService(input entity.GetAdminListInput) entity.CustomResponse
	UpdateSrqCost(input entity.UpdateSrqCostInput) entity.CustomResponse
	CreateChildServiceRequest(input entity.CreateChildServiceRequestInput, img_paths []string) entity.CustomResponse
}

type AdminService struct {
	userRepository *repository.UserRepository
	//notification    *repository.NotificationRepository
	//otpRepository   *repository.OTPRepository
	adminRepository   *repository.AdminRepository
	roleRepository    *repository.RoleRepository
	serviceRepository *repository.ServiceRepository
	commonRepository  *repository.ConfigRepository
}

// auth-jwt
func AdminServiceInit(notification *repository.NotificationRepository, userRepository *repository.UserRepository, otpRepository *repository.OTPRepository, adminRepository *repository.AdminRepository, roleRepository *repository.RoleRepository, serviceRepository *repository.ServiceRepository, commonRepository *repository.ConfigRepository) *AdminService {
	return &AdminService{
		//notification:    notification,
		userRepository: userRepository,
		//otpRepository:   otpRepository,
		adminRepository:   adminRepository,
		roleRepository:    roleRepository,
		serviceRepository: serviceRepository,
		commonRepository:  commonRepository,
	}
}

func (useServ *AdminService) ValidateCustomerService(input entity.ValidateCustomer) (response entity.CustomResponse) {
	if strings.ToUpper(input.Account_Status) == "ACCEPTED" || strings.ToUpper(input.Account_Status) == "REJECTED" {
		validateResponse, err := useServ.userRepository.UserApproval(input)
		if err == nil {
			if strings.ToUpper(input.Account_Status) == "ACCEPTED" {
				response.Resultcode = 0
				response.Message = dto.SuccessMessageUserApproval
				response.Payload = validateResponse
			} else if strings.ToUpper(input.Account_Status) == "REJECTED" {
				response.Resultcode = 0
				response.Message = dto.SuccessMessageUserRejection
				response.Payload = validateResponse
			}
		} else {
			response.Resultcode = 1
			response.Message = err.Error()
			response.Payload = validateResponse
		}
	} else {
		response.Resultcode = 1
		response.Message = "Invalid status"
		//response.Payload = validateResponse
	}
	return response
}

func (adminserv *AdminService) SignupAdminService(input entity.SignUpAdminInput, img_path string) (response entity.CustomResponse) {
	tx, errt := adminserv.adminRepository.Begin()
	if errt != nil {
		response.Resultcode = 1
		response.Message = errt.Error()
		log.Println("AdminService: SignupAdminService: Input: ", input, "Error ", errt.Error())
		return response
	}
	resp, err := adminserv.adminRepository.SignUpAdmin(tx, input, img_path)
	if err != nil {
		tx.Rollback()
		response.Resultcode = 1
		response.Message = dto.ErrorMessageSignUpAdmin + err.Error()
		log.Println("AdminService: SignupAdminService: Input: ", input, "Error ", err.Error())
		return response
	}

	commitError := tx.Commit()
	if commitError != nil {
		response.Resultcode = 1
		response.Message = dto.ErrorMessageSignUpAdmin + commitError.Error()
		log.Println("AdminService: SignupAdminService: Input: ", input, "Error ", commitError.Error())
		return response
	} else {
		if input.Role_ID == 1 {
			response.Message = dto.SuccessMessageSignUpTech
		} else {
			response.Message = dto.SuccessMessageSignUpAdmin
		}
		response.Resultcode = 0
		response.Payload = resp
		return response
	}
}

func (adminServ *AdminService) GetAdminListService(input entity.GetAdminListInput) (response entity.CustomResponse) {
	getAdminResponse, err := adminServ.adminRepository.FetchAdminList(input) //db (query execution in repository)
	if err == nil {
		fmt.Print("e::", len(getAdminResponse.AdminLists))
		if len(getAdminResponse.AdminLists) == 0 || input.Page > getAdminResponse.Page {
			response.Resultcode = 0
			response.Message = dto.ErrorMessageAdminsFetch + "No records found for this category"
			return response
		} else {
			response.Resultcode = 0
			response.Message = dto.SuccessMessageAdminsFetch
			response.Payload = getAdminResponse
		}

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

	return response
}

func (adminServ *AdminService) UpdateSrqCost(input entity.UpdateSrqCostInput) (response entity.CustomResponse) {
	tx, errt := adminServ.adminRepository.Begin()

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

	resp, err := adminServ.adminRepository.SrqCostUpdate(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.ErrorMessageUpdateCost + err.Error()
	} else {
		response.Resultcode = 0
		response.Message = dto.SuccessMessageUpdateCost
		response.Payload = resp
	}

	return response
}

func (adminServ *AdminService) CreateChildServiceRequest(input entity.CreateChildServiceRequestInput, img_paths []string) (response entity.CustomResponse) {

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

	}

	resp, err := adminServ.adminRepository.CreateChildSrq(tx, input, img_paths)

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

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

}
func (adminServ *AdminService) AdminDashboardService(input entity.AdminDashboardInput) (response entity.CustomResponse) {

	resp, err := adminServ.adminRepository.AdminDashboard(input)
	if err != nil {
		response.Resultcode = 1
		response.Message = dto.ErrorMessageAdminDashboard + err.Error()
	} else {
		response.Resultcode = 0
		response.Message = dto.SuccessMessageAdminDashboard
		response.Payload = resp
	}
	return response
}
