package com.hepl.tunefortwo.service.impl;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import com.hepl.tunefortwo.dto.ArtistDto;
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.entity.Language;
import com.hepl.tunefortwo.entity.MixtureMaster;
import com.hepl.tunefortwo.repository.ArtistRepository;
import com.hepl.tunefortwo.repository.LanguageRepository;
import com.hepl.tunefortwo.service.ArtistService;
import com.hepl.tunefortwo.utils.AppMessages;

import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ArtistServiceImpl implements ArtistService {
	
	private final ArtistRepository artistRepository;
	
	public ArtistServiceImpl(ArtistRepository artistRepository ) {
		
		this.artistRepository = artistRepository;
	}

	
	@Override
	public java.util.List<ArtistMaster> getAllArtist() {
		log.info("Get all Language");
		return artistRepository.findAll();
	}

	
	@Override
	public ArtistMaster getArtistById(String id) {
	    log.info("Fetching artist by ID: {}", id);
	    ArtistMaster artistfound=artistRepository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, AppMessages.ARTIST_NOT_FOUND));
	    return artistfound;
	}
	
//	@Override
//	public Artist updateArtist(String id, Artist updatedArtist) {
//	    log.info("Update Artist with ID: {}", id);
//	    Artist artist = artistRepository.findById(id)
//	            .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, AppMessages.RESOURCE_NOT_FOUND));
//	    artist.setName(updatedArtist.getName());
//	    return artistRepository.save(artist);
//	}

	@Override
	public ArtistMaster setArtistById(Double price, String id) throws ResponseStatusException{
		ArtistMaster artist = artistRepository.findById(id)
				.orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, AppMessages.ARTIST_NOT_FOUND));
		artist.setPrice(price);
		ArtistMaster updatedData = artistRepository.save(artist);
		return updatedData;
	}

	@Override
	public List<ArtistMaster> addCollection() {
	    List<ArtistMaster> findAllArtists = artistRepository.findAll();
	    
	    if(findAllArtists.isEmpty()) {
	        ArtistMaster artistm = new ArtistMaster();
	        artistm.setArtist(Artist.Male);
	        artistm.setPrice(1000);
	        artistRepository.save(artistm);
	        
	        ArtistMaster artistf = new ArtistMaster(); 
	        artistf.setArtist(Artist.Female); 
	        artistf.setPrice(1000);
	        artistRepository.save(artistf);
	        
	        List<ArtistMaster> finalList = new ArrayList<>();
	        finalList.add(artistm);
	        finalList.add(artistf);
	        
	        return finalList;
	    } else {
	        throw new DataIntegrityViolationException("Artist collection already has data");
	    }
	}


	



	}
