
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.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());
    }

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


    // create news by employeesId
    @PostMapping("/news/employees/{employeeId}")
    public News createNews(@PathVariable (value = "employeeId") int employeeId,
                           @Valid @RequestBody News news) {
        Employee employee = employeeBusiness.findById(employeeId);
        news.setEmployee(employee);
        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.findById(newsId).isPresent()) {
            throw  new ResourceNotFoundException("News", "id", newsId);
        }
        return newsBusiness.updateNews(newsId,newsRequest);
    }

    //delete news by employeeId and newsId
    @DeleteMapping("/employees/{employeeId}/news/{newsId}")
    public ResponseEntity<?> deleteNews(@PathVariable (value = "employeeId") int employeeId,
                                           @PathVariable (value = "newsId") int newsId) {
        if(!newsBusiness.findByIdAndEmployeeId(employeeId,newsId).isPresent()){
            throw  new ResourceNotFoundException("News","newsId",newsId);
        }
        return ResponseEntity.ok(newsBusiness.deleteNews(employeeId,newsId));
    }
}
