package com.utopiaindustries.controller;

import com.utopiaindustries.auth.QCRole;
import com.utopiaindustries.model.ctp.FinishedItem;
import com.utopiaindustries.model.ctp.StitchedItemWrapper;
import com.utopiaindustries.service.BundleService;
import com.utopiaindustries.service.InventoryAccountService;
import com.utopiaindustries.service.InventoryService;
import com.utopiaindustries.service.LocationService;
import com.utopiaindustries.util.StringUtils;
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
@QCRole
@RequestMapping( "/quality-control" )
public class QualityControlController {

    private final InventoryAccountService inventoryAccountService;
    private final BundleService bundleService;
    private final LocationService locationService;
    private final InventoryService inventoryService;

    public QualityControlController(InventoryAccountService inventoryAccountService, BundleService bundleService, LocationService locationService, InventoryService inventoryService) {
        this.inventoryAccountService = inventoryAccountService;
        this.bundleService = bundleService;
        this.locationService = locationService;
        this.inventoryService = inventoryService;
    }

    @GetMapping
    public String homePage( Model model ){
        return "redirect:/quality-control/qc-finished-items";
    }

    /*
     *  get stitching inventory accounts
     * */
    @GetMapping( "/inventory-accounts" )
    public String getInventoryAccounts(  @RequestParam( value = "id", required = false ) String id,
                                         @RequestParam( value = "title", required = false) String title,
                                         @RequestParam( value = "active", required = false ) String active,
                                         @RequestParam( value = "created-by", required = false ) String createdBy,
                                         @RequestParam( value = "start-date", required = false ) String startDate,
                                         @RequestParam( value = "end-date", required = false ) String endDate,
                                         @RequestParam( value = "site-id", required = false ) String siteId,
                                         @RequestParam( value = "count", required = false ) Long count,
                                         Model model ) {
        // 24 for Packaging
        model.addAttribute("accounts", inventoryAccountService.getInventoryAccounts(id, title, active, createdBy, startDate, endDate, siteId, count, "PROCESS", "4", false));
        model.addAttribute("locations", locationService.findAll() );
        return "/quality-control/inventory-accounts";
    }

    @GetMapping( "/qc-finished-items" )
    public String getFinishedItems( @RequestParam(value = "id", required = false ) String id,
                                    @RequestParam(value = "item-id", required = false ) String itemId,
                                    @RequestParam( value = "sku", required = false ) String sku,
                                    @RequestParam( value = "start-date", required = false) String startDate,
                                    @RequestParam( value = "end-date", required = false ) String endDate,
                                    @RequestParam( value = "job-card-id", required = false ) String jobCardId,
                                    @RequestParam( value = "count", required = false, defaultValue = "100") Long count,
                                    Model model ){
        LocalDate startDate1 = StringUtils.isNullOrEmpty(startDate) ? LocalDate.now().minusDays(30) : LocalDate.parse(startDate);
        LocalDate endDate1 = StringUtils.isNullOrEmpty(endDate) ? LocalDate.now() : LocalDate.parse(endDate);
        model.addAttribute("startDate", startDate1);
        model.addAttribute("endDate", endDate1);
        List<FinishedItem> itemList = bundleService.getFinishedItem( id, itemId, sku, startDate1.toString(), endDate1.toString(), jobCardId ,count );
        model.addAttribute("items", itemList ) ;
        return "/quality-control/qc-items-list";
    }

    @GetMapping( "/qc-finished-item" )
    public String getAddFinishedItemForQCForm( Model model ){
        // 4 for packaging
        model.addAttribute("accounts", inventoryAccountService.findInventoryAccounts(5L) );
        model.addAttribute("wrapper", new StitchedItemWrapper() );
        return "/quality-control/qc-items-form";
    }

    @PostMapping( "/qc-finished-item" )
    public String postFinishedItemsAfterQc( @ModelAttribute StitchedItemWrapper wrapper,
                                            RedirectAttributes redirectAttributes ){
        try {
            inventoryService.createFinishedItemsAgainstStitchedItems( wrapper );
            redirectAttributes.addFlashAttribute("success",  " Finished Items Are Generated Against Stitched Items" );
        }catch ( Exception ex ){
            redirectAttributes.addFlashAttribute("error",  ex.getMessage() );
        }
        return "redirect:/quality-control/qc-finished-items";
    }
}
