package com.utopiaindustries.controller;

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

@Controller
@StoreRole
@RequestMapping( "/store" )
public class StoreController {

    private final InventoryAccountService inventoryAccountService;
    private final StoreService storeService;
    private final LocationService locationService;

    public StoreController(InventoryAccountService inventoryAccountService, StoreService storeService, LocationService locationService) {
        this.inventoryAccountService = inventoryAccountService;
        this.storeService = storeService;
        this.locationService = locationService;
    }

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

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


    @PostMapping( "/store-items" )
    public String packagingItems( @ModelAttribute FinishedItemWrapper wrapper,
                                  RedirectAttributes redirectAttributes,
                                  Model model ){
        try {
            storeService.createStoreItems( wrapper );
            redirectAttributes.addFlashAttribute("success", "Items Successfully received !" );
        } catch ( Exception  e ){
            redirectAttributes.addFlashAttribute("error", e.getMessage() );
        }
        return "redirect:/store/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:/store/inventory-accounts?id=&title=&active=1&created-by=&start-date=&end-date=&site-id=&site-title=&count=100";
        }
        model.addAttribute("accounts", inventoryAccountService.findInventoryAccounts(9L));
        model.addAttribute("locations", locationService.findAll() );
        return "/store/inventory-accounts";
    }
}
