package com.sikey.wa04.api.user.client.rest;

import com.sikey.wa04.business.application.service.user.DeviceService;
import com.sikey.wa04.business.infrastructure.configuration.ProjectContextConstant;
import com.sikey.wa04.business.infrastructure.dto.request.device.BindDeviceRequest;
import com.sikey.wa04.business.infrastructure.dto.request.device.GetAppManageRequest;
import com.sikey.wa04.business.infrastructure.dto.request.device.UnbindDeviceRequest;
import com.sikey.wa04.business.infrastructure.dto.request.device.UpdateAppManageRequest;
import com.sikey.wa04.business.infrastructure.dto.response.device.DeviceBindResponse;
import com.sikey.wa04.business.infrastructure.dto.response.device.GetAppManageResponse;
import com.sikey.wa04.common.BaseController;
import com.sikey.wa04.common.dto.header.GenericManufacturerHeader;
import com.sikey.wa04.common.interceptor.Authenticated;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(ProjectContextConstant.USER_CONTEXT_URL + "/user/child/device")
public class DeviceController extends BaseController {
    private final DeviceService deviceService;

    public DeviceController(DeviceService deviceServiceImpl) {
        this.deviceService = deviceServiceImpl;
    }

    /**
     * 绑定设备
     *
     * @param request 绑定设备
     * @return
     */
    @Authenticated
    @PostMapping("/bind")
    public DeviceBindResponse bind(@Valid @RequestBody BindDeviceRequest request) throws Exception {
        request.setHeader(genericManufacturerHeader());
        return deviceService.bind(request);
    }

    @Authenticated
    @PostMapping("/unbind")
    public void unbind(@Valid @RequestBody UnbindDeviceRequest request) throws Exception {
        GenericManufacturerHeader genericManufacturerHeader = genericManufacturerHeader();
        deviceService.unbind(request, genericManufacturerHeader);
    }

    /**
     * 获取三方应用管理
     *
     * @param ticket
     * @return
     */
    @GetMapping("/getAppManageList")
    public GetAppManageResponse getAppManageList(@RequestParam("ticket") String ticket) {
        GenericManufacturerHeader genericManufacturerHeader = genericManufacturerHeader();
        return deviceService.getAppManageList(new GetAppManageRequest(ticket),genericManufacturerHeader, false);
    }

    @Authenticated
    @PostMapping("/updateAppManage")
    public void updateAppManage(@Valid @RequestBody UpdateAppManageRequest request) {
        GenericManufacturerHeader genericManufacturerHeader = genericManufacturerHeader();
        deviceService.updateAppManage(request,genericManufacturerHeader);
    }
}
