
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 com.itsol.quantrivanphong.report.leaveform.controller.Notification;
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);
    }

    @GetMapping("/catalogi/{catalogiId}/news/{newsId}")
    public News getNewsByCatalogiId(@PathVariable(value="catalogiId") int catalogiId,
                                          @PathVariable(value="newsId") int newsId){
        return newsBusiness.findNewsByIdAndAndCatalogiId(newsId,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("/HR/{employeeId}/catalogies/{catalogiId}/news")
    public ResponseEntity 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);
        newsBusiness.save(news);
        return ResponseEntity.ok(new Notification(200, "Success"));
    }

    // Edit news by EmployeeId
    @PutMapping("HR/news/{newsId}")
    public News updateNews(@PathVariable (value = "newsId") int newsId,
                           @Valid @RequestBody News newsRequest) {

        if (newsBusiness.findNewsById(newsId)== null) {
            throw  new ResourceNotFoundException("News", "id", newsId);
        }
        return newsBusiness.updateNews(newsId,newsRequest);
    }

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