package com.zioinfo.esn.controller;

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

@RestController
@RequestMapping("/api/hcore")
@RequiredArgsConstructor
public class HCoreController {
    private final HCoreService service;

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

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

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