package com.utopiaindustries.controller;

import com.utopiaindustries.auth.PackagingRole;
import com.utopiaindustries.service.InventoryAccountService;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;

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


    private final InventoryAccountService inventoryAccountService;
    private final LocationService locationService;

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


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

    @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";
    }
}
