package com.hepl.tunefortwo.service.impl;

import java.io.IOException;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import com.hepl.tunefortwo.dto.PaymentDto;
import com.hepl.tunefortwo.entity.DeliveryDate;
import com.hepl.tunefortwo.entity.FileType;
import com.hepl.tunefortwo.entity.Payment;
import com.hepl.tunefortwo.repository.DeliveryDateRepository;
import com.hepl.tunefortwo.repository.PaymentRepository;
import com.hepl.tunefortwo.service.FileService;
import com.hepl.tunefortwo.service.PaymentService;
import com.hepl.tunefortwo.utils.AppMessages;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class PaymentServiceImpl implements PaymentService {
	
	private final PaymentRepository repository;
	private final DeliveryDateRepository deliveryDateRepository;
	
	
	public PaymentServiceImpl(PaymentRepository repository,DeliveryDateRepository deliveryDateRepository) {
		this.repository = repository;
		this.deliveryDateRepository = deliveryDateRepository;
		
	}
	
	   
	public Payment savepayment(PaymentDto dto)  {
		log.info("save payment");
		Payment payment = new Payment();
		payment.setEmailId(dto.getEmailId());
		payment.setName(dto.getName());
		payment.setUpiPath(dto.getFilePath());
		return repository.save(payment);
	}
	
	public void updatePayment(PaymentDto dto,String id) {
		log.info("save payment");
		Payment payment = repository.findById(id).orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND,AppMessages.RESOURCE_NOT_FOUND));
		
		if (dto.getEmailId() != null && !dto.getEmailId().isEmpty()) {
		        payment.setEmailId(dto.getEmailId());
		    }
		    if (dto.getName() != null && !dto.getName().isEmpty()) {
		        payment.setName(dto.getName());
		    }
		    if (dto.getFilePath() != null && !dto.getFilePath().isEmpty()) {
		        payment.setUpiPath(dto.getFilePath());
		    }
		 repository.save(payment);
	}
	
	public void delete(String id) {
		log.info("Delete payment : "+ id);
		Payment payment = repository.findById(id).orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND,AppMessages.RESOURCE_NOT_FOUND));
		repository.deleteById(id);
	}


	@Override
	public PaymentDto getPaymentById(String id) {
		log.info("Get payment : "+ id);
		Payment payment = repository.findById(id).orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND,AppMessages.RESOURCE_NOT_FOUND));
		PaymentDto dto = new PaymentDto();
		dto.setEmailId(payment.getEmailId());
		dto.setName(payment.getName());
		dto.setFilePath(payment.getUpiPath());
		return dto;
	}


	@Override
	public List<Payment> getAllPayments() {
		log.info("Get all payments ");
		return repository.findAll();
	}
	
	@Override
	public  double calculateDeliveryPayment(LocalDate clientDate) {	
		List<DeliveryDate> findAllDeliveryDates = deliveryDateRepository.findAll();
		if (!findAllDeliveryDates.isEmpty()) {
		    DeliveryDate deliveryDateFound = findAllDeliveryDates.get(0);
		    double basePrice = deliveryDateFound.getPriceForTotalDays();
		    double variablePrice = deliveryDateFound.getPricePerDay();
		    System.out.println("basePrice-------------------------------" + basePrice);

		    LocalDate currentDate = LocalDate.now(); 

		    long difference = ChronoUnit.DAYS.between(currentDate, clientDate);
		    String estimatedDaysStr = deliveryDateFound.getEstimatedDays();
		    long estimatedDays = Long.parseLong(estimatedDaysStr);
		    
		    if (difference > estimatedDays) {
		        return 00.00; 
		    } else if (difference == estimatedDays) {
		        return basePrice;
		    } else {
		        double multiplier = (estimatedDays - difference) * variablePrice; 
		        return basePrice + multiplier;
		    }
		}

	    else {
	    	 throw new DataIntegrityViolationException("Delivery date Base Price not available");
	    }
	    }
	

}
