package com.example.demo.product.controller;

import com.example.demo.common.AppDefaultsProperties;
import com.example.demo.product.dto.ProductDtos;
import com.example.demo.product.service.ProductService;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/products")
public class ProductController {

    private final ProductService productService;
    private final AppDefaultsProperties defaults;

    public ProductController(ProductService productService, AppDefaultsProperties defaults) {
        this.productService = productService;
        this.defaults = defaults;
    }

    @GetMapping
    public ResponseEntity<?> search(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                    @RequestParam(name = "kw", required = false) String kw,
                                    @RequestParam(name = "categoryId", required = false) Long categoryId,
                                    @RequestParam(name = "page", defaultValue = "1") int page,
                                    @RequestParam(name = "size", defaultValue = "50") int size) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Page<ProductDtos.ProductListItem> result = productService.search(sid, kw, categoryId, Math.max(page - 1, 0), size);
        java.util.Map<String, Object> body = new java.util.HashMap<>();
        body.put("list", result.getContent());
        return ResponseEntity.ok(body);
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> detail(@PathVariable("id") Long id) {
        return productService.findDetail(id)
                .<ResponseEntity<?>>map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    @PostMapping
    public ResponseEntity<?> create(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                    @RequestHeader(name = "X-User-Id", required = false) Long userId,
                                    @RequestBody ProductDtos.CreateOrUpdateProductRequest req) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Long uid = (userId == null ? defaults.getUserId() : userId);
        Long id = productService.create(sid, uid, req);
        java.util.Map<String, Object> body = new java.util.HashMap<>();
        body.put("id", id);
        return ResponseEntity.ok(body);
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable("id") Long id,
                                    @RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                    @RequestHeader(name = "X-User-Id", required = false) Long userId,
                                    @RequestBody ProductDtos.CreateOrUpdateProductRequest req) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Long uid = (userId == null ? defaults.getUserId() : userId);
        productService.update(id, sid, uid, req);
        return ResponseEntity.ok().build();
    }
}


