package com.haha.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.haha.admin.annotation.RequirePermission;
import com.haha.admin.dto.DeviceQueryDTO;
import com.haha.common.annotation.Log;
import com.haha.common.enums.OperationType;
import com.haha.common.vo.PageResult;
import com.haha.common.vo.Result;
import com.haha.entity.Device;
import com.haha.service.DeviceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 设备管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/devices")
@RequiredArgsConstructor
public class DeviceController {

    private final DeviceService deviceService;

    /**
     * 分页查询设备列表
     * @param queryDTO 查询参数
     * @return 设备列表
     */
    @RequirePermission("device:read")
    @GetMapping("/list")
    public Result<PageResult<Device>> list(DeviceQueryDTO queryDTO) {
        queryDTO.validate();

        IPage<Device> devicePage = deviceService.getPage(
                queryDTO.getPage(),
                queryDTO.getPageSize(),
                queryDTO.getDeviceId(),
                queryDTO.getShopId(),
                queryDTO.getStatus()
        );

        return Result.success("查询成功", PageResult.of(devicePage));
    }

    /**
     * 获取设备详情
     * @param id 设备ID
     * @return 设备详情
     */
    @RequirePermission("device:read")
    @GetMapping("/{id}")
    public Result<Device> getById(@PathVariable Long id) {
        Device device = deviceService.getDetailById(id);
        if (device == null) {
            return Result.error(404, "设备不存在");
        }
        return Result.success("查询成功", device);
    }

    /**
     * 获取设备统计数据
     * @return 统计数据
     */
    @RequirePermission("device:read")
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics() {
        Map<String, Object> statistics = deviceService.getStatistics();
        return Result.success("查询成功", statistics);
    }

    /**
     * 远程开门
     * @param id 设备ID
     * @param params 开门参数
     * @return 操作结果
     */
    @RequirePermission("device:update")
    @Log(module = "设备管理", operation = OperationType.OTHER, summary = "远程开门")
    @PostMapping("/{id}/open")
    public Result<Void> openDoor(@PathVariable Long id, @RequestBody Map<String, Object> params) {
        String doorIndex = (String) params.getOrDefault("doorIndex", "A");
        boolean success = deviceService.openDoor(id, doorIndex);
        return success ? Result.success("开门指令已发送", null) : Result.error(500, "开门失败");
    }

    /**
     * 设置温度
     * @param id 设备ID
     * @param params 温度参数
     * @return 操作结果
     */
    @RequirePermission("device:update")
    @PutMapping("/{id}/temperature")
    public Result<Void> setTemperature(@PathVariable Long id, @RequestBody Map<String, Object> params) {
        Double temperature = Double.valueOf(params.get("temperature").toString());
        boolean success = deviceService.setTemperature(id, temperature);
        return success ? Result.success("温度设置成功", null) : Result.error(500, "设置失败");
    }

    /**
     * 设置音量
     * @param id 设备ID
     * @param params 音量参数
     * @return 操作结果
     */
    @RequirePermission("device:update")
    @PutMapping("/{id}/volume")
    public Result<Void> setVolume(@PathVariable Long id, @RequestBody Map<String, Object> params) {
        Integer volume = Integer.valueOf(params.get("volume").toString());
        boolean success = deviceService.setVolume(id, volume);
        return success ? Result.success("音量设置成功", null) : Result.error(500, "设置失败");
    }
}
