package com.zioinfo.signage.controller;

import com.zioinfo.signage.common.ApiResponse;
import com.zioinfo.signage.domain.SignBoardVo;
import com.zioinfo.signage.service.SignBoardService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

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

    @GetMapping
    public ApiResponse<List<SignBoardVo>> 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<SignBoardVo> get(@PathVariable Long id) {
        return ApiResponse.ok(service.get(id));
    }

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

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

    @PutMapping("/{id}")
    public ApiResponse<SignBoardVo> update(@PathVariable Long id, @RequestBody SignBoardVo 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);
    }
}
