package com.zioinfo.signage.controller;

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

@RestController
@RequestMapping("/api/gateways")
@RequiredArgsConstructor
public class GatewayController {
    private final GatewayService service;

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

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

    @PostMapping
    public ApiResponse<GatewayVo> create(@RequestBody GatewayVo v) {
        return ApiResponse.ok("장치 등록 완료", service.create(v));
    }

    @PutMapping("/{id}")
    public ApiResponse<GatewayVo> update(@PathVariable Long id, @RequestBody GatewayVo v) {
        return ApiResponse.ok("장치 수정 완료", service.update(id, v));
    }

    @PutMapping("/{id}/status")
    public ApiResponse<Void> updateStatus(@PathVariable Long id, @RequestBody Map<String, String> body) {
        service.updateStatus(id, body.getOrDefault("status", "OFFLINE"));
        return ApiResponse.ok("상태 업데이트 완료", null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        service.delete(id);
        return ApiResponse.ok("장치 삭제 완료", null);
    }
}
