package com.utopiaindustries.controller;

import com.utopiaindustries.auth.PackagingRole;
import com.utopiaindustries.model.ctp.FinishedItemWrapper;
import com.utopiaindustries.service.InventoryAccountService;
import com.utopiaindustries.service.LocationService;
import com.utopiaindustries.service.PackagingService;
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;

@Controller
@PackagingRole
@RequestMapping("/packaging" )
public class PackagingController {

    private final InventoryAccountService inventoryAccountService;
    private final PackagingService packagingService;
    private final LocationService locationService;

    public PackagingController(InventoryAccountService inventoryAccountService, PackagingService packagingService, LocationService locationService) {
        this.inventoryAccountService = inventoryAccountService;
        this.packagingService = packagingService;
        this.locationService = locationService;
    }

    @GetMapping
    public String showHome(Model model ){
        return "redirect:/packaging/receive-inventory";
    }

    @GetMapping("/receive-inventory")
    public String packagingItemReceive( Model model ){
        model.addAttribute("accounts", inventoryAccountService.findInventoryAccounts(6L));
        model.addAttribute("wrapper", new FinishedItemWrapper() );
        return "/packaging/receive-inventory-form";
    }


    @PostMapping( "/packaging-items" )
    public String packagingItems( @ModelAttribute FinishedItemWrapper wrapper,
                                                 RedirectAttributes redirectAttributes,
                                                 Model model ){
        try {
            packagingService.createPackagingItem( wrapper );
            redirectAttributes.addFlashAttribute("success", "Items Successfully received !" );
        } catch ( Exception  e ){
            redirectAttributes.addFlashAttribute("error", e.getMessage() );
        }
        return "redirect:/packaging/receive-inventory";
    }

    @GetMapping( "/inventory-accounts" )
    public String showInventoryAccounts(@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 ){
        if(StringUtils.isNullOrEmpty( active )){
            return "redirect:/packaging/inventory-accounts?id=&title=&active=1&created-by=&start-date=&end-date=&site-id=&site-title=&count=100";
        }
        model.addAttribute("accounts", inventoryAccountService.getInventoryAccounts( id, title, active, createdBy, startDate, endDate, siteId, count , null, null,true ) );
        model.addAttribute("locations", locationService.findAll() );
        return "/packaging/inventory-accounts";
    }
}
