package com.psg.dlsysinfo.dl_sysinfo_server.controller;

import com.psg.dlsysinfo.dl_sysinfo_server.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/admin/manage")
@PreAuthorize("hasRole('ADMIN')")
public class ManagementController {

    @Autowired
    private DeviceService deviceService;

    @DeleteMapping("/devices/{deviceId}")
    public ResponseEntity<?> deleteDevice(@PathVariable Long deviceId) {
        try {
            deviceService.deleteDeviceAndChildren(deviceId); // Implement this in your service
            return ResponseEntity.ok("✅ Device deleted successfully");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("❌ Failed to delete device: " + e.getMessage());
        }
    }

    @PutMapping("/devices/{deviceId}/client")
    public ResponseEntity<?> updateDeviceClient(
            @PathVariable Long deviceId,
            @RequestBody Map<String, Long> body) {
        Long newClientId = body.get("clientId");
        deviceService.reassignClient(deviceId, newClientId);
        return ResponseEntity.ok("Client updated");
    }


    // Other create/delete endpoints can go here
}
