package research.loghunter.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import research.loghunter.dto.ServerDto;
import research.loghunter.service.ServerService;
import research.loghunter.service.SftpService;

import java.util.List;

@RestController
@RequestMapping("/api/servers")
@RequiredArgsConstructor
public class ServerController {

    private final ServerService serverService;
    private final SftpService sftpService;

    @GetMapping
    public ResponseEntity<List<ServerDto>> findAll() {
        return ResponseEntity.ok(serverService.findAll());
    }

    @GetMapping("/active")
    public ResponseEntity<List<ServerDto>> findAllActive() {
        return ResponseEntity.ok(serverService.findAllActive());
    }

    @GetMapping("/{id}")
    public ResponseEntity<ServerDto> findById(@PathVariable Long id) {
        return ResponseEntity.ok(serverService.findById(id));
    }

    @PostMapping
    public ResponseEntity<ServerDto> create(@RequestBody ServerDto dto) {
        return ResponseEntity.ok(serverService.create(dto));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ServerDto> update(@PathVariable Long id, @RequestBody ServerDto dto) {
        return ResponseEntity.ok(serverService.update(id, dto));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        serverService.delete(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 연결 테스트
     */
    @PostMapping("/{id}/test-connection")
    public ResponseEntity<SftpService.ConnectionTestResult> testConnection(@PathVariable Long id) {
        return ResponseEntity.ok(sftpService.testConnection(id));
    }
}
