
package com.itsol.quantrivanphong.access.homepage.controller;

import com.itsol.quantrivanphong.access.homepage.business.CatalogiBusiness;
import com.itsol.quantrivanphong.access.homepage.business.NewsBusiness;
import com.itsol.quantrivanphong.exception.ResourceNotFoundException;
import com.itsol.quantrivanphong.manager.employee.business.EmployeeBusiness;
import com.itsol.quantrivanphong.model.Catalogi;
import com.itsol.quantrivanphong.model.Employee;
import com.itsol.quantrivanphong.model.News;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("/api")
public class NewsController {
    @Autowired
    private NewsBusiness newsBusiness;

    @Autowired
    private CatalogiBusiness catalogiBusiness;

    @Autowired
    private EmployeeBusiness employeeBusiness;
    // get All news
    @GetMapping("/news")
    public ResponseEntity<List<News>> getAllNews(){
        return ResponseEntity.ok(newsBusiness.findAllNews());
    }

    @GetMapping("/news/{newsId}")
    public News getNewsById(@PathVariable(value = "newsId") int newsId){

        return newsBusiness.findNewsById(newsId);
    }
    // get news by employeeId
    @GetMapping("/employees/{employeeId}/news")
    public List<News> getAllNewsByEmployeeId(@PathVariable(value = "employeeId") int employeeId) {
        return newsBusiness.findByEmployeeId(employeeId);
    }
    // get News by catalogiId
    @GetMapping("/catalogi/{catalogiId}/news")
    public List<News> getAllNewsByCatalogiId(@PathVariable(value="catalogiId") int catalogiId){
        return newsBusiness.findByCatalogiId(catalogiId);
    }

    // get LatestNews by catalogiId
    @GetMapping("/catalogi/{catalogiId}/latestNews")
    public List<News> getLatestNews(@PathVariable(value="catalogiId") int catalogiId){
        return newsBusiness.findLatestNews(catalogiId);
    }

    //get Latest news
    @GetMapping("/catalogi/latestNews")
    public List<News> getAllLatestNews(){
        return newsBusiness.findAllLatestNews();
    }

    @GetMapping("/employees/{employeeId}/news/{newsId}")
    public News getAllNewsByEmployeeId(@PathVariable(value = "employeeId") int employeeId,
                                       @PathVariable (value = "newsId") int newsId) {
        return newsBusiness.findNewsByIdAndEmployeeId(newsId,employeeId);
    }

    // create news by employeesId, categoriId
    @PostMapping("/employees/{employeeId}/catalogies/{catalogiId}/news")
    public News createNews(@PathVariable (value = "employeeId") int employeeId,
                           @PathVariable (value = "catalogiId") int catalogiId,
                           @Valid @RequestBody News news) {
        Employee employee =  employeeBusiness.findById(employeeId);
        Catalogi catalogi = catalogiBusiness.findCatalogiById(catalogiId);
        if(employee == null){
            throw  new  ResourceNotFoundException("Employee" ,"employeeId",employeeId);
        }
        if(catalogi==null){
            throw  new  ResourceNotFoundException("Catalogi" ,"catalogiId",catalogiId);
        }

        news.setEmployee(employee);
        news.setCatalogi(catalogi);
        return newsBusiness.save(news);
    }

    // Edit news by EmployeeId
    @PutMapping("/employees/{employeeId}/news/{newsId}")
    public News updateNews(@PathVariable (value = "employeeId") int employeeId,
                           @PathVariable (value = "newsId") int newsId,
                           @Valid @RequestBody News newsRequest) {
        if(employeeBusiness.findById(employeeId)==null) {
            throw new ResourceNotFoundException("Employee" ,"employeeId",employeeId);
        }
        if (newsBusiness.findNewsById(newsId)== null) {
            throw  new ResourceNotFoundException("News", "id", newsId);
        }
        return newsBusiness.updateNews(newsId,newsRequest);
    }

    //delete news by employeeId and newsId
    @DeleteMapping("/HR/catagori/{catagoriId}/news/{newsId}")
    public ResponseEntity<?> deleteNews(@PathVariable (value = "catagoriId") int catagoriId,
                                           @PathVariable (value = "newsId") int newsId) {
        if(newsBusiness.findNewsByIdAndAndCatalogiId(newsId,catagoriId)== null){
            throw  new ResourceNotFoundException("News","newsId",newsId);
        }
        return ResponseEntity.ok(newsBusiness.deleteNews(newsId,catagoriId));
    }
}
