package com.example.video_downloader.controllers;

import com.example.video_downloader.dto.SaveNewVideoRequest;
import com.example.video_downloader.entity.Video;
import com.example.video_downloader.services.VideoService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping(path="/video")
@CrossOrigin(origins = "http://localhost:3000")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Data
    private class Response {
        private Long id;
    }

    @PostMapping(path = "/videos")
    public Response saveNewVideo(@RequestBody SaveNewVideoRequest request){

        Response response = new Response();
        Video video = videoService.saveNewVideo(request);
        response.setId(video.getId());

        return response;

    }

    @GetMapping(path = "/all")
    public ResponseEntity<List<Video>> getAllVideos(){
        return ResponseEntity.ok(videoService.getVideos());
    }

    @GetMapping(path = "/{id}")
    public ResponseEntity<Video> getVideoById(@PathVariable Long id){
        return ResponseEntity.ok(videoService.getVideoById(id));

    }
}
