package com.hepl.tunefortwo.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpStatus;
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.RequestBody;
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.google.common.base.Optional;
import com.hepl.tunefortwo.config.i18n.Translator;
import com.hepl.tunefortwo.dto.ArtistDto;
import com.hepl.tunefortwo.dto.FormRequestDto;
import com.hepl.tunefortwo.dto.GenericData;
import com.hepl.tunefortwo.dto.GenericResponse;
import com.hepl.tunefortwo.dto.LanguageRequestDto;
import com.hepl.tunefortwo.entity.Artist;
import com.hepl.tunefortwo.entity.ArtistMaster;
import com.hepl.tunefortwo.entity.DeliveryDate;
import com.hepl.tunefortwo.service.ArtistService;
import com.hepl.tunefortwo.service.LanguageService;
import com.hepl.tunefortwo.utils.AppMessages;
import com.hepl.tunefortwo.utils.JwtUtils;

import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.mail.MessagingException;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "Manage Artist", description = "")
//@SecurityRequirement(name = "Bearer Authentication")
@RestController
@RequestMapping("/v1/artistmaster")
@Slf4j
public class ArtistMasterController {
	private final ArtistService artistService;
	private final Translator translator;
	private final JwtUtils jwtUtils;
	
	public ArtistMasterController(ArtistService artistService,Translator translator,JwtUtils jwtUtils) {
		this.artistService=artistService; 
		this.translator = translator;
		this.jwtUtils = jwtUtils;
	}
	 @GetMapping("/getall")
		public GenericResponse getAllArtist() {
			log.info("Get all Artist");
			GenericResponse response = new GenericResponse(true);
			GenericData data = new GenericData();
			data.setArtist(artistService.getAllArtist());
			response.setData(data);
			return response;
		}

	 
//	    @GetMapping("/{id}")
//	    public GenericResponse getArtistById(@PathVariable String id) {
//	        log.info("Fetching forms by Artistamount...");
//	        GenericResponse response = new GenericResponse(true);
//	   	 	GenericData data = new GenericData();
//           data.setArtistMaster(artistService.getArtistById(id));)
//   	 	   response.setData(data);
//	   	 	return response;
//	    }
	 @GetMapping("/{id}")
	 public GenericResponse getArtistById(@PathVariable String id) {
	     log.info("Fetching artist by ID: {}", id);
	     
	     GenericResponse response = new GenericResponse(true);
	     GenericData data = new GenericData();
	     data.setArtistMaster(artistService.getArtistById(id));
	     
	     response.setData(data);
	     
	     return response;
	 }

	    


	 @GetMapping("/createartistcollection")
		public GenericResponse createArtistCollection(Authentication authentication) {

			if (authentication == null || !authentication.isAuthenticated()) {
				throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Unauthorized");
			}
			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) {
			GenericResponse response = new GenericResponse(true);
			GenericData data = new GenericData();
			List<ArtistMaster> addedData = artistService.addCollection();
			data.setArtist(addedData);
			response.setData(data);
			response.setMessage(translator.toLocale(AppMessages.DELIVERYDATEDATA_ADDED_SUCCESSFULLY));
			return response;
			} else {
				throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "You dont have the previllege to perform this action");
			}
		}
	 
	
	 @Operation(description = "Update Artist based on id")
	 @PutMapping("/{id}")
	 public GenericResponse updateArtist(@PathVariable String id, @RequestBody ArtistDto artistDto) {
	     log.info("Update Artist with ID: {}", id);
	     Double artistPrice = artistDto.getPrice();
	     ArtistMaster updatedArtist = artistService.setArtistById(artistPrice, id);
	     GenericResponse response = new GenericResponse(true);
	     GenericData data= new GenericData();
	     List<ArtistMaster> updatedData = new ArrayList();
	     updatedData.add(updatedArtist);
	     data.setArtist(updatedData);
	     response.setData(data);
	     response.setMessage("Artist Updated Successfully");
	     return response;
	 }

//	 @PutMapping("/{id}")
//		public GenericResponse setArtistById(@RequestParam Double price, @PathVariable String id) {
//			log.info("Artist Price");
//			GenericResponse response = new GenericResponse(true);
//			artistService.setArtistById(price, id);
//			response.setMessage(translator.toLocale(AppMessages.ARTIST_UPDATED_SUCCESSFULLY));
//			return response;
//		}
}
