package com.utopiaindustries.controller;

import com.utopiaindustries.auth.PurchaseOrderCTPRole;
import com.utopiaindustries.model.ctp.POsDetails;
import com.utopiaindustries.service.InventoryAccountService;
import com.utopiaindustries.service.PurchaseOrderService;
import com.utopiaindustries.service.ReportingService;
import com.utopiaindustries.service.SummaryInventoryReportService;
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;


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

    @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, Model model){

        model.addAttribute("allPOs",  reportingService.getAllPOs(poName));
        return "/reporting/po-report";
    }

    @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 includeJobCard,
                                                                  @RequestParam(required = false, defaultValue = "true") boolean includeStoreDetails,
                                                                  Model model) throws Exception{
        return purchaseOrderService.generatePOPdf(pOsDetails, model, includeJobCard, includeStoreDetails);
    }
}
