package com.example.video_downloader;

import com.example.video_downloader.entity.StatusEnum;
import com.example.video_downloader.entity.Video;
import com.example.video_downloader.repositories.VideoRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;


import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;


import java.util.List;

@SpringBootTest
public class VideoDownloaderApplicationTest {

	@Autowired
	VideoRepository videoRepository;


	private MockMvc mockMvc;

	@BeforeEach
	public void setUp(WebApplicationContext webApplicationContext) {
		this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
        .build();
	}

	@Test
	void contextLoads() {
		Video v = new Video();
		v.setStatus(StatusEnum.NEW);
		videoRepository.save(v);

		List<Video> findByStatusList = videoRepository.findByStatus(StatusEnum.NEW);
		Assert.isTrue(findByStatusList.size() == 10,"Anyad");
	}

	@Test
	void insertListIntoVideoTest() throws Exception {
		mockMvc.perform(post("/v2/videos").contentType(MediaType.APPLICATION_JSON)
				.content("{\"url\":\"https://www.youtube.com/watch?v=swiGWilMqeQ&list=RDswiGWilMqeQ\"}")).andDo(MockMvcResultHandlers.print())
				.andExpect(status().isBadRequest())
				.andReturn();;
	}

	@Test
	void insertVideoTest() throws Exception {
		MvcResult mvcResult = mockMvc.perform(post("/v2/videos").contentType(MediaType.APPLICATION_JSON)
						.content("{\"url\":\"https://www.youtube.com/watch?v=pOq0pJ6QLhE\"}")).andDo(print())
				.andExpect(status().isOk())
				.andReturn();
		System.out.println(mvcResult.getResponse().getContentAsString());
	}
}