package com.eactive.testmaster.socket.controller;


import com.eactive.testmaster.socket.entity.NettyServerEntity;
import com.eactive.testmaster.socket.service.NettyServerService;
import java.util.List;
import javax.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/socket")
public class NettyTCPServerRestController {

    private final NettyServerService nettyServerService;

    public NettyTCPServerRestController(NettyServerService nettyServerService) {
        this.nettyServerService = nettyServerService;
    }

    @GetMapping
    @Secured("ROLE_MANAGE_NETTY")
    public ResponseEntity<Page<NettyServerEntity>> listNettyServers(
        @ModelAttribute("serverSearch") NettyTCPServerSearch serverSearch,
        @RequestParam(defaultValue = "0") int page,
        @RequestParam(defaultValue = "10") int size
    ) {
        Pageable pageable = PageRequest.of(page, size);
        Page<NettyServerEntity> servers = nettyServerService.findAll(serverSearch.buildSpecification(), pageable);
        return ResponseEntity.ok(servers);
    }

    @PostMapping
    @Secured("ROLE_MANAGE_NETTY")
    public ResponseEntity<NettyServerEntity> createNettyServer(@RequestBody @Valid NettyServerEntity server) {
        try {
            NettyServerEntity created = nettyServerService.createServer(server);
            return ResponseEntity.ok(created);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    @PostMapping("/update/{id}")
    @Secured("ROLE_MANAGE_NETTY")
    public ResponseEntity<NettyServerEntity> updateNettyServer(
        @PathVariable Long id,
        @RequestBody @Valid NettyServerEntity server
    ) {
        try {
            NettyServerEntity updated = nettyServerService.updateServer(id, server);
            return ResponseEntity.ok(updated);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    @GetMapping("/{id}")
    @Secured("ROLE_MANAGE_NETTY")
    public ResponseEntity<NettyServerEntity> getNettyServer(@PathVariable Long id) {
        try {
            NettyServerEntity server = nettyServerService.findById(id);
            return ResponseEntity.ok(server);
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }


    @PostMapping("/delete")
    public ResponseEntity<Void> deleteTCPServer(@RequestBody List<Long> ids) throws InterruptedException {
        for (Long id : ids) {
            nettyServerService.deleteServer(id);
        }
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{id}/start")
    @Secured("ROLE_MANAGE_NETTY")
    public ResponseEntity<Void> startServer(@PathVariable Long id) {
        try {
            nettyServerService.startServer(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    @PostMapping("/{id}/stop")
    @Secured("ROLE_MANAGE_NETTY")
    public ResponseEntity<Void> stopServer(@PathVariable Long id) {
        try {
            nettyServerService.stopServer(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}
