package research.loghunter.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import research.loghunter.dto.PatternDto;
import research.loghunter.service.PatternService;

import java.util.List;

@RestController
@RequestMapping("/api/patterns")
@RequiredArgsConstructor
public class PatternController {

    private final PatternService patternService;

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

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

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

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

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

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

    @PostMapping("/test")
    public ResponseEntity<PatternService.PatternTestResult> testPattern(
            @RequestParam String regex,
            @RequestParam String sampleText) {
        return ResponseEntity.ok(patternService.testPattern(regex, sampleText));
    }
}
