package com.zioinfo.esn.controller;

import com.zioinfo.esn.common.ApiResponse;
import com.zioinfo.esn.domain.TagBindingVo;
import com.zioinfo.esn.service.TagBindingService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * e-paper ESL 태그 ↔ 선반 위치 ↔ 상품 바인딩 관리
 * 핵심 플로우: 태그 스캔 → 상품 선택 → 위치 지정 → bind() → 가격 변경 시 자동 큐
 */
@RestController
@RequestMapping("/api/tag-binding")
@RequiredArgsConstructor
public class TagBindingController {
    private final TagBindingService service;

    @GetMapping
    public ApiResponse<List<TagBindingVo>> list(
            @RequestParam(required = false) String tenantCode,
            @RequestParam(required = false) Long storeId,
            @RequestParam(required = false) String bindStatus,
            @RequestParam(required = false) String keyword) {
        return ApiResponse.ok(service.list(tenantCode, storeId, bindStatus, keyword));
    }

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

    @GetMapping("/device/{deviceId}")
    public ApiResponse<TagBindingVo> getByDevice(@PathVariable String deviceId) {
        return ApiResponse.ok(service.getByDeviceId(deviceId));
    }

    /** 태그에 상품+위치 바인딩 */
    @PostMapping
    public ApiResponse<TagBindingVo> bind(@RequestBody TagBindingVo vo) {
        return ApiResponse.ok(service.bind(vo));
    }

    @PutMapping("/{id}")
    public ApiResponse<TagBindingVo> update(@PathVariable Long id, @RequestBody TagBindingVo vo) {
        vo.setId(id);
        return ApiResponse.ok(service.bind(vo));
    }

    /** 태그 언바인딩 (선반에서 태그 제거 시) */
    @PutMapping("/{id}/unbind")
    public ApiResponse<Void> unbind(@PathVariable Long id) {
        service.unbind(id);
        return ApiResponse.ok("언바인딩 완료", null);
    }
}
