package com.example.demo.product.controller;

import com.example.demo.common.AppDefaultsProperties;
import com.example.demo.product.repo.CategoryRepository;
import com.example.demo.product.repo.UnitRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class MetadataController {

    private final UnitRepository unitRepository;
    private final CategoryRepository categoryRepository;
    private final AppDefaultsProperties defaults;

    public MetadataController(UnitRepository unitRepository, CategoryRepository categoryRepository, AppDefaultsProperties defaults) {
        this.unitRepository = unitRepository;
        this.categoryRepository = categoryRepository;
        this.defaults = defaults;
    }

    @GetMapping("/api/product-units")
    public ResponseEntity<?> listUnits(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Map<String, Object> body = new HashMap<>();
        body.put("list", unitRepository.listByShop(sid));
        return ResponseEntity.ok(body);
    }

    @GetMapping("/api/product-categories")
    public ResponseEntity<?> listCategories(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Map<String, Object> body = new HashMap<>();
        body.put("list", categoryRepository.listByShop(sid));
        return ResponseEntity.ok(body);
    }
}


