package com.utopiaindustries.controller;

import com.utopiaindustries.auth.PurchaseOrderCTPRole;
import com.utopiaindustries.model.ctp.JobCard;
import com.utopiaindustries.model.ctp.PurchaseOrderCTP;
import com.utopiaindustries.service.PurchaseOrderCTPService;
import com.utopiaindustries.util.StringUtils;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.time.LocalDate;
import java.util.List;

@Controller
@RequestMapping("/purchase-order")
@PurchaseOrderCTPRole

public class PurchaseOrderCTPController {
    private final PurchaseOrderCTPService purchaseOrderCTPService;

    public PurchaseOrderCTPController(PurchaseOrderCTPService purchaseOrderCTPService) {
        this.purchaseOrderCTPService = purchaseOrderCTPService;
    }

    @GetMapping
    public String showJobCardList(  @RequestParam( value = "purchaseOrderCode", required = false ) String purchaseOrderCode,
                                    @RequestParam( value = "articleName", required = false ) String articleName,
                                    @RequestParam( value = "created-start-date", required = false ) String createdStartDate,
                                    @RequestParam( value = "created-end-date", required = false ) String createdEndDate,
                                    @RequestParam( value = "limit" , required = false) Long limit,
                                    Model model ){

        LocalDate startDate = StringUtils.isNullOrEmpty(createdStartDate) ? LocalDate.now().minusDays(30) : LocalDate.parse(createdStartDate);
        LocalDate endDate = StringUtils.isNullOrEmpty(createdEndDate) ? LocalDate.now() : LocalDate.parse(createdEndDate);
        model.addAttribute("purchaseOrder", purchaseOrderCTPService.getAllPurchaseOrderCtp(purchaseOrderCode, articleName, startDate.toString(), endDate.toString(), limit) );
        model.addAttribute("startDate", startDate);
        model.addAttribute("endDate", endDate);
        return "/purchaseOrder/purchase-order-list";
    }

    @GetMapping( "/new" )
    public String showPurchaseOrderCTPForm( Model model ){
        model.addAttribute("purchaseOrder", purchaseOrderCTPService.createNewPurchaseOrderCTP() );
        return "/purchaseOrder/purchase-order-form";
    }

    @GetMapping( value = "/edit/{id}" )
    public String showJobCardEditForm( @PathVariable("id") long id,
                                       Model model ){
        model.addAttribute("purchaseOrder", purchaseOrderCTPService.searchPurchaseOrderById( id ) );
        return "/purchaseOrder/purchase-order-form";
    }

    /*
     *  draft
     * */
    @PostMapping( value ="/edit" , params = "user=draft" )
    public String saveJobCard( @ModelAttribute PurchaseOrderCTP purchaseOrderCTP,
                               RedirectAttributes redirectAttributes,
                               Model model ){
        try {
            purchaseOrderCTP.setStatus( PurchaseOrderCTP.Status.DRAFT.name() );
            purchaseOrderCTPService.save( purchaseOrderCTP );
            redirectAttributes.addFlashAttribute("success", "Successfully saved!" );
        } catch ( Exception ex ){
            redirectAttributes.addFlashAttribute("error", ex.getMessage() );
        }
        return "redirect:/purchase-order";
    }

    @PostMapping( value ="/edit" , params = "user=post" )
    public String postJobCard( @ModelAttribute PurchaseOrderCTP purchaseOrderCTP,
                               RedirectAttributes redirectAttributes,
                               Model model ){

        try {
            purchaseOrderCTP.setStatus( PurchaseOrderCTP.Status.POSTED.name() );
            purchaseOrderCTPService.save( purchaseOrderCTP );
            redirectAttributes.addFlashAttribute("success", "Successfully saved!" );
        } catch ( Exception ex ){
            redirectAttributes.addFlashAttribute("error", ex.getMessage() );
        }
        return "redirect:/purchase-order";
    }

    @GetMapping( "/store-items/{id}" )
    public String getPOStoreItems( @PathVariable("id") long poId,
                                                     Model model ){
        model.addAttribute("storeItems",  purchaseOrderCTPService.getStoreItemsByPoId( poId ));
        return "/reporting/po-store-items-table";
    }

}
