package com.utopiaindustries.controller;

import com.utopiaindustries.auth.PurchaseOrderCTPRole;
import com.utopiaindustries.model.ctp.POsDetails;
import com.utopiaindustries.service.*;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/po-status")
@PurchaseOrderCTPRole
public class POStatusController {

    private final ReportingService reportingService;
    private final PurchaseOrderService purchaseOrderService;
    private final JobCardItemService jobCardItemService;


    public POStatusController(ReportingService reportingService, PurchaseOrderService purchaseOrderService, JobCardItemService jobCardItemService) {
        this.reportingService = reportingService;
        this.purchaseOrderService = purchaseOrderService;
        this.jobCardItemService = jobCardItemService;
    }

    @GetMapping
    public String homePage( Model model ){
        return "redirect:/po-status/all-pos";
    }

    @GetMapping( "/all-pos")
    public String poReport(@RequestParam(value = "poName", required = false) String poName,
                           @RequestParam(value = "size", required = false) String size,
                           @RequestParam(value = "color", required = false) String color,
                           @RequestParam(value = "count", required = false, defaultValue = "100") Long count,
                           Model model){

        model.addAttribute("allSize",jobCardItemService.getAllSizesOFItems());
        model.addAttribute("allColor",jobCardItemService.getAllColorOFItems());
        model.addAttribute("allPOs",  reportingService.getAllPOs(poName, size, color, count));
        return "/reporting/po-report";
    }

    @GetMapping( "/po-items")
    public String poReport(@RequestParam("poId") long poId,
                           @RequestParam(value = "size", required = false) String size,
                           @RequestParam(value = "color", required = false) String color,
                           Model model){

        model.addAttribute("allSize",jobCardItemService.getAllSizesOFItems());
        model.addAttribute("allColor",jobCardItemService.getAllColorOFItems());
        model.addAttribute("poJobcardItems",  reportingService.getPoItemsSizeOrColor(poId, size, color));
        return "/reporting/po-jobcard-items-table";
    }

    @GetMapping( value = "/po-report-view/{poId}" )
    public String showJobCardDetail(@PathVariable("poId") long poId, @RequestParam(value = "select-date", required = false) String selectDate ,
                                    Model model ){
        model.addAttribute("allJobCard",  reportingService.getAllPoJobCards(poId, selectDate));
        return "/reporting/po-job-card-report";
    }



    @GetMapping(value = "/generate-po-pdf", produces = MediaType.APPLICATION_PDF_VALUE)
    public ResponseEntity<InputStreamResource> sendPoAndReturnPdf(@ModelAttribute POsDetails pOsDetails,
                                                                  @RequestParam(required = false, defaultValue = "true") boolean includeItemsDetail,
                                                                  @RequestParam(required = false, defaultValue = "true") boolean includeStoreDetails,
                                                                  @RequestParam String color,
                                                                  @RequestParam String size,
                                                                  Model model) throws Exception{
        return purchaseOrderService.generatePOPdf(pOsDetails, model, includeItemsDetail, includeStoreDetails, size, color);
    }
}
