package com.nebula.erp.inventory.controller;

import com.nebula.erp.inventory.service.PublicInventoryService;
import com.nebula.erp.inventory.utility.ApiResponseStructure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/public")
public class PublicInventoryController {

    @Autowired
    private PublicInventoryService publicInventoryService;

    // GET /api/public/inventories?tenant=biocom_Tenant
    @GetMapping("/inventories")
    public ResponseEntity<ApiResponseStructure<Map<String, Object>>> getInventoriesByTenant(
            @RequestParam(value = "tenant", required = true) String tenant) {
        try {
            List<Map<String, Object>> items = publicInventoryService.getInventoriesByTenant(tenant);

            Map<String, Object> data = new LinkedHashMap<>();
            data.put("total", items.size());
            data.put("items", items);

            return ResponseEntity.ok(new ApiResponseStructure<>("success",
                    HttpStatus.OK.value(), "Inventories retrieved successfully.", data));

        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(new ApiResponseStructure<>("error",
                            HttpStatus.BAD_REQUEST.value(), e.getMessage(), null));
        }
    }
}