package com.izouma.nineth.web;

import com.izouma.nineth.domain.Activity;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.repo.ActivityRepo;
import com.izouma.nineth.service.ActivityService;
import com.izouma.nineth.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/activity")
@AllArgsConstructor
public class ActivityController extends BaseController {
    private ActivityService activityService;
    private ActivityRepo activityRepo;

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public Activity save(@RequestBody Activity record) {
        return activityService.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<Activity> all(@RequestBody PageQuery pageQuery) {
        return activityService.all(pageQuery);
    }

    @GetMapping("/get/{id}")
    public Activity get(@PathVariable Long id) {
        return activityService.findById(id);
    }


    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        activityRepo.softDelete(id);
    }

    @PostMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, @RequestBody PageQuery pageQuery) throws IOException {
        List<Activity> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @GetMapping("/hasRead")
    public Map<String, Boolean> hasRead(){
        return activityService.hasRead();
    }

    @GetMapping("/contraction/{projectId}")
    public List<Activity> contraction(@PathVariable Integer projectId){
        return activityService.contraction(projectId);
    }

}

