package com.realnet.OpenAi.Services;

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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import com.realnet.OpenAi.Models.UnsplashPhotoResponse;

@Service
public class UnsplashService {

	@Value("${unsplash.api.url}")
	private String apiUrl;

	@Value("${unsplash.access.key}")
	private String accessKey;

	public List<String> getImageUrls(String keyword) {
		RestTemplate restTemplate = new RestTemplate();
		String uri = UriComponentsBuilder.fromHttpUrl(apiUrl).queryParam("query", keyword)
				.queryParam("client_id", accessKey).queryParam("per_page", 10).toUriString();

		UnsplashPhotoResponse response = restTemplate.getForObject(uri, UnsplashPhotoResponse.class);

		List<String> imageUrls = new ArrayList<>();
		if (response != null && response.getResults() != null) {
			for (UnsplashPhotoResponse.Result r : response.getResults()) {
				if (r.getUrls() != null && r.getUrls().getRegular() != null) {
					imageUrls.add(r.getUrls().getRegular());
				}
			}
		}

		return imageUrls;
	}
}
