package com.common.controller;


import com.common.entity.DeviceUnknown;
import com.common.service.DeviceUnknownService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/device-unknown")
@RequiredArgsConstructor
@Validated
public class DeviceUnknownController {

    private final DeviceUnknownService deviceUnknownService;

    /**
     * 创建设备记录
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createDevice(@Valid @RequestBody DeviceUnknown device) {
        Long id = deviceUnknownService.createDevice(device);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("id", id);
        result.put("message", "设备记录创建成功");
        return ResponseEntity.ok(result);
    }

    /**
     * 批量创建设备记录
     */
    @PostMapping("/batch")
    public ResponseEntity<Map<String, Object>> batchCreateDevices(@Valid @RequestBody List<DeviceUnknown> devices) {
        int count = deviceUnknownService.batchCreateDevices(devices);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("count", count);
        result.put("message", "批量创建设备记录成功");
        return ResponseEntity.ok(result);
    }

    /**
     * 根据ID获取设备信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getDeviceById(@PathVariable Long id) {
        DeviceUnknown device = deviceUnknownService.getDeviceById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", device);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据IP查询设备
     */
    @GetMapping("/ip/{deviceIp}")
    public ResponseEntity<Map<String, Object>> getDevicesByIp(@PathVariable String deviceIp) {
        List<DeviceUnknown> devices = deviceUnknownService.getDevicesByIp(deviceIp);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", devices);
        result.put("total", devices.size());
        return ResponseEntity.ok(result);
    }

    /**
     * 根据组织ID查询设备
     */
    @GetMapping("/organization/{organizationId}")
    public ResponseEntity<Map<String, Object>> getDevicesByOrganizationId(@PathVariable Integer organizationId) {
        List<DeviceUnknown> devices = deviceUnknownService.getDevicesByOrganizationId(organizationId);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", devices);
        result.put("total", devices.size());
        return ResponseEntity.ok(result);
    }

    /**
     * 分页查询设备
     */
    @GetMapping("/page")
    public ResponseEntity<Map<String, Object>> getDevicesByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        List<DeviceUnknown> devices = deviceUnknownService.getDevicesByPage(pageNum, pageSize);
        long total = deviceUnknownService.getTotalCount();

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", devices);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        result.put("total", total);
        result.put("pages", (int) Math.ceil((double) total / pageSize));
        return ResponseEntity.ok(result);
    }

    /**
     * 更新设备信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateDevice(
            @PathVariable Long id,
            @Valid @RequestBody DeviceUnknown device) {
        device.setId(id);
        boolean success = deviceUnknownService.updateDevice(device);

        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        result.put("message", success ? "设备更新成功" : "设备更新失败");
        return ResponseEntity.ok(result);
    }

    /**
     * 删除设备
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteDevice(@PathVariable Long id) {
        boolean success = deviceUnknownService.deleteDevice(id);
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        result.put("message", success ? "设备删除成功" : "设备删除失败");
        return ResponseEntity.ok(result);
    }
}
