package com.example.video_downloader.services;

import com.example.video_downloader.dto.SaveNewVideoRequest;
import com.example.video_downloader.entity.StatusEnum;
import com.example.video_downloader.entity.Video;
import com.example.video_downloader.repositories.VideoRepository;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideoService {

    @Autowired
    private VideoRepository videoRepository;

    public Video saveNewVideo(SaveNewVideoRequest request) {
        if (request.getUrl().contains("list")) throw new InvalidParameterException("");
        Video video  = new Video();
        video.setStatus(StatusEnum.NEW);
        video.setUrl(request.getUrl());

        videoRepository.save(video);
        return video;
    }

    public List<Video> getVideos(){
        return videoRepository.findAll();
    }

    public Video getVideoById(Long id){
        //FIXME Throwolj EntityNotFoundException-t
        return videoRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Video not found with id: " + id));
    }


}
