package com.hepl.tunefortwo.controller;

import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import com.hepl.tunefortwo.config.i18n.Translator;
import com.hepl.tunefortwo.dto.GenericData;
import com.hepl.tunefortwo.dto.GenericResponse;
import com.hepl.tunefortwo.service.MixtureMasterService;
import com.hepl.tunefortwo.utils.AppMessages;
import com.hepl.tunefortwo.utils.JwtUtils;

import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "Set Price For MixtureMaster", description = "")
//@SecurityRequirement(name = "Bearer Authentication")
@RestController
@RequestMapping("/v1/mixturemaster")
@Slf4j
public class MixtureMasterController {

	private final MixtureMasterService mixtureMasterService;
	private final JwtUtils jwtUtils;
	private final Translator translator;

	public MixtureMasterController(MixtureMasterService mixtureMasterService, Translator translator,JwtUtils jwtUtils) {
		this.mixtureMasterService = mixtureMasterService;
		this.translator = translator;
		this.jwtUtils = jwtUtils;
	}

	@GetMapping()
	public GenericResponse getmixtureMasterPrice() {
		log.info("Get mixturemaster Price");
		GenericResponse response = new GenericResponse(true);
		GenericData data = new GenericData();
		data.setMixtureMasters(mixtureMasterService.getmixtureMasterPrice());
		response.setData(data);
		return response;
	}
	
	@GetMapping("/{id}")
	public GenericResponse getmixtureMasterPrice(@PathVariable String id) {
		log.info("Get mixturemaster Price");
		GenericResponse response = new GenericResponse(true);
		GenericData data = new GenericData();
//		data.setMixtureMasters(mixtureMasterService.getmixtureMasterPrice(id));
		data.setMixtureMaster(mixtureMasterService.getmixtureMaster(id));
		response.setData(data);
		return response;
	}

	@PutMapping("/{id}")
	public GenericResponse setmixtureMasterById(@RequestParam Double price, @PathVariable String id,Authentication authentication) {
		boolean isAdmin = false;
	    if (authentication instanceof JwtAuthenticationToken) {
	        JwtAuthenticationToken jwtAuthToken = (JwtAuthenticationToken) authentication;
	        Jwt jwt = jwtAuthToken.getToken();
	        String tokenValue = jwt.getTokenValue();
	        Claims claims = jwtUtils.extractClaims(tokenValue);
	        String email = claims.getSubject();
	        Map<String, Object> userDetails = (Map<String, Object>) claims.get("userDetails");
	        if (userDetails != null) {
	            String roleId = (String) userDetails.get("roleId");
	            String userId = (String) userDetails.get("id");
	            if (roleId.equals("1")) {
	                isAdmin = true;
	            }
	        }
	    }

	    if (!isAdmin) {
	        ResponseEntity<String> response = ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");
	        throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Unauthorized");
	    }
		
		log.info("Set mixturemaster Price");

		GenericResponse response = new GenericResponse(true);
		mixtureMasterService.setmixtureMasterById(price, id);
		response.setMessage(translator.toLocale(AppMessages.MIXTUREMASTER_PRICE_UPDATED_SUCCESSFULLY));
		return response;
	}
}
