package com.zioinfo.mall.product;

import com.zioinfo.mall.common.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/** 상품 API — /api/mall/product. GET 공개(스토어프론트), 변경 MANAGER+. */
@RestController
@RequestMapping("/api/mall/product")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService service;

    @GetMapping
    public ApiResponse<Map<String, Object>> list(
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String occasion,
            @RequestParam(required = false) String flowerType,
            @RequestParam(required = false) java.math.BigDecimal maxPrice,
            @RequestParam(required = false) String sort,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.ok(service.search(categoryId, keyword, status, occasion, flowerType, maxPrice, sort, page, size));
    }

    @GetMapping("/{id}")
    public ApiResponse<MallProduct> get(@PathVariable Long id) {
        return ApiResponse.ok(service.detail(id));
    }

    @PostMapping
    public ApiResponse<MallProduct> create(@RequestBody MallProduct p) {
        return ApiResponse.ok(service.create(p));
    }

    @PutMapping("/{id}")
    public ApiResponse<MallProduct> update(@PathVariable Long id, @RequestBody MallProduct p) {
        return ApiResponse.ok(service.update(id, p));
    }

    @PutMapping("/{id}/status")
    public ApiResponse<Void> status(@PathVariable Long id, @RequestBody Map<String, String> req) {
        service.updateStatus(id, req.get("status"));
        return ApiResponse.ok(null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        service.delete(id);
        return ApiResponse.ok(null);
    }
}
