package com.hepl.tunefortwo.controller;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

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.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.FiledDto;
import com.hepl.tunefortwo.dto.GenericData;
import com.hepl.tunefortwo.dto.GenericResponse;
import com.hepl.tunefortwo.dto.IsMandatory;
import com.hepl.tunefortwo.dto.PaymentDto;
import com.hepl.tunefortwo.entity.FileType;
import com.hepl.tunefortwo.entity.Filed;
import com.hepl.tunefortwo.service.FiledService;
import com.hepl.tunefortwo.service.PaymentService;
import com.hepl.tunefortwo.utils.AppMessages;

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 = "Create and Manage Filed", description = "")
//@SecurityRequirement(name = "Bearer Authentication")
@RestController
@RequestMapping("/v1/Filed")
@Slf4j
public class FiledController {
	
	private final FiledService filedService;
	private final Translator translator;
	
	public FiledController(FiledService filedService, Translator translator) {
		this.filedService = filedService;
		this.translator = translator;
	}
	
	@PostMapping(consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
	public GenericResponse addPayemnt(@ModelAttribute FiledDto data)
			throws MessagingException, IOException {
		log.info("add Filed");
		filedService.addFiled(data);
		GenericResponse response = new GenericResponse(true);
		response.setMessage(translator.toLocale(AppMessages.FILED_SAVED));
		return response;
	}
	
	@GetMapping("/{id}")
    public GenericResponse getFiledById(@PathVariable String id) {
        log.info("Get Filed");
        GenericResponse response = new GenericResponse(true);
        GenericData data = new GenericData();
        data.setFiled(filedService.getFiledById(id));
        response.setData(data);
        return response;
    }
	
	@GetMapping("/")
    public GenericResponse getAllFileds() {
        log.info("Get All Filed");
        GenericResponse response = new GenericResponse(true);
        GenericData data = new GenericData();
        Set<String> namesToRemove = Set.of("Mood", "Song Delivery","Record your story audio");
        List<Filed>allFields=filedService.getAllFiled();
        if
        (allFields.size() > 4) {            
        	allFields = allFields.subList(4, allFields.size());         }
        else
        { 
        	allFields.clear();    
        }
        List<Filed> filteredList = allFields.stream()
                .filter(f -> !namesToRemove.contains(f.getName()))
                .collect(Collectors.toList());
        
        data.setFileds(filteredList);
        
        response.setData(data);
        return response;
    }
	
	@Operation(description = "Update Filed based on id")
	@PutMapping(value="/{id}")
	public GenericResponse updateFiled(@PathVariable String id,
			@RequestParam IsMandatory isMandatory) throws IOException {
		log.info("Update Filed ... {}", id);
		filedService.updateFiled(isMandatory, id);
		GenericResponse response = new GenericResponse(true);
		response.setMessage(translator.toLocale(AppMessages.FILED_UPDATED_SUCCESSFULLY));
		return response;
	}

}
