package com.utopiaindustries.controller;

import com.utopiaindustries.auth.FinishingRole;
import com.utopiaindustries.dao.ctp.FinishedItemDAO;
import com.utopiaindustries.model.ctp.FinishedItem;
import com.utopiaindustries.model.ctp.FinishedItemWrapper;
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
@FinishingRole
@RequestMapping("/finishing")
public class FinishingController {

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

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

    @GetMapping
    public String showHome(Model model) {
        return "redirect:/finishing/finished-items";
    }

    @GetMapping("/finished-items")
    public String showFinishedItems(@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 = "status", required = false) String status,
                                    @RequestParam(value = "count", required = false) 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, status, count);
        model.addAttribute("items", itemList);

        return "finishing/finished-item-list";
    }


    /*
     *  get finishing 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, defaultValue = "100") Long count,
                                       Model model) {
        // 5 for Finishing
        model.addAttribute("accounts", inventoryAccountService.getInventoryAccounts(id, title, active, createdBy, startDate, endDate, siteId, count, "PROCESS", "5", false));
        model.addAttribute("locations", locationService.findAll());
        return "/finishing/inventory-accounts";
    }

    @GetMapping("segregate-inventory")
    public String segregateFinishedItems(Model model) {
        model.addAttribute("wrapper", new FinishedItemWrapper());
        return "/finishing/segregate-inventory";
    }


    @PostMapping("segregate-inventory")
    public String segregateFinishItemsInventory(@ModelAttribute FinishedItemWrapper wrapper,
                                                RedirectAttributes redirectAttributes,
                                                Model model) {
        try {
            inventoryService.segregateFinishedItems(wrapper, wrapper.getQaStatus());
            redirectAttributes.addFlashAttribute("success", "Items Successfully saved !");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", e.getMessage());
        }
        return "redirect:/finishing/segregate-inventory";
    }

}
