package com.hepl.tunefortwo.controller;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hepl.tunefortwo.config.i18n.Translator;
import com.hepl.tunefortwo.dto.AddCommentDto;
import com.hepl.tunefortwo.dto.FormRequestDto;
import com.hepl.tunefortwo.dto.GenericData;
import com.hepl.tunefortwo.dto.GenericResponse;
import com.hepl.tunefortwo.dto.OrderSummaryDto;
import com.hepl.tunefortwo.entity.OrderSummary;
import com.hepl.tunefortwo.service.OrderSummaryService;
import com.hepl.tunefortwo.utils.AppMessages;
import com.itextpdf.text.DocumentException;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.mail.MessagingException;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "Create and Manage orderSummary", description = "")
//@SecurityRequirement(name = "Bearer Authentication")
@RestController
@RequestMapping("/v1/orderSummary")
@Slf4j
public class OrderSummaryController {
	private final OrderSummaryService orderSummaryService ;
	
	public OrderSummaryController(OrderSummaryService orderSummaryService) {
		this.orderSummaryService=orderSummaryService;
	}
	
	@PutMapping("/orderSummary/{id}")
    public GenericResponse addorderSummary(@RequestParam String id,
    		                               @RequestParam (value = "languageAmount" , required = false) double languageAmout,
    		                               @RequestParam (value="versionAmount", required=false)double versionAmount,
    		                               @RequestParam (value="masterAmount",required=false)double masterAmount,
    		                               @RequestParam (value="deliverydateAmount",required=false)double deliverydateAmount,
    		                               @RequestParam (value="artistAmount",required=false)double artistAmount)
                            throws IOException, DocumentException, MessagingException {
		                   
				double totalAmount = languageAmout+versionAmount+masterAmount+deliverydateAmount+artistAmount;
				orderSummaryService.updateorderSummary( id , languageAmout,  versionAmount,  masterAmount,
						 deliverydateAmount,  totalAmount,artistAmount);
				GenericResponse response=new GenericResponse(true);
				response.setMessage((AppMessages.ORDERSUMMARY_UPDATED));
				return response;
		
	}
	
	  @GetMapping("/{id}")
	    public GenericResponse getorderSummary(@PathVariable String id) {
	        	 GenericResponse response = new GenericResponse(true);
	        	 OrderSummary ordersummary=orderSummaryService.getordersummarybyid(id); 
	        	 GenericData data = new GenericData();
	        	 data.setOrderSummary(ordersummary);
	        	 response.setData(data);  
	        	 return response;  
	  }
	  @PostMapping(consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
	  public GenericResponse saveOrderSummary(@ModelAttribute OrderSummaryDto orderSummary ) throws MessagingException {
		  OrderSummary newSummary= new OrderSummary();
		  GenericResponse response = new GenericResponse(true);
		  if (!Double.isNaN(orderSummary.getLanguageAmount()) &&
				    !Double.isNaN(orderSummary.getVersionAmount()) &&
				    !Double.isNaN(orderSummary.getMasterAmount()) &&
				    !Double.isNaN(orderSummary.getDeliverydateAmount()) &&
				    !Double.isNaN(orderSummary.getArtistAmount())) {
			  		double total = orderSummary.getLanguageAmount()+orderSummary.getVersionAmount()+orderSummary.getMasterAmount()+orderSummary.getDeliverydateAmount()+orderSummary.getArtistAmount();
			  		
				    OrderSummary summary = orderSummaryService.saveOrderSummary(orderSummary,total);
				   
				    response.setMessage(AppMessages.ORDERSUMMARY_SAVED + "id : " + summary.getId());
				    System.out.println("ArtistAmount"+total);
				    GenericData data = new GenericData();
				    data.setOrderSummary(summary);
				    response.setData(data);
				    return response;
				    
				} else {
				    response.setMessage("Enter valid data");
				    return response;
				}

	
		  
	  }
}
