package com.agent.management.controller;

import com.agent.management.common.Result;
import com.agent.management.model.entity.HermesErrorPattern;
import com.agent.management.service.HermesErrorPatternService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Hermes 错误嗅探模式管理接口。
 *
 * <p>路径前缀 {@code /api/hermes-error-patterns}，与 {@code AiModelController} 风格一致
 * （动词式 path：{@code /{id}/edit}、{@code /{id}/delete}、{@code /{id}/toggle}）。</p>
 */
@RestController
@RequestMapping("/api/hermes-error-patterns")
@RequiredArgsConstructor
public class HermesErrorPatternController {

    private final HermesErrorPatternService service;

    @GetMapping
    public Result<List<HermesErrorPattern>> list() {
        return Result.success(service.list());
    }

    @PostMapping
    public Result<HermesErrorPattern> create(@RequestBody PatternReq req) {
        HermesErrorPattern entity = new HermesErrorPattern();
        entity.setName(req.getName());
        entity.setPattern(req.getPattern());
        entity.setDescription(req.getDescription());
        entity.setEnabled(req.getEnabled());
        return Result.success(service.create(entity));
    }

    @PostMapping("/{id}/edit")
    public Result<HermesErrorPattern> update(@PathVariable Long id, @RequestBody PatternReq req) {
        HermesErrorPattern entity = new HermesErrorPattern();
        entity.setName(req.getName());
        entity.setPattern(req.getPattern());
        entity.setDescription(req.getDescription());
        entity.setEnabled(req.getEnabled());
        return Result.success(service.update(id, entity));
    }

    @PostMapping("/{id}/delete")
    public Result<Void> delete(@PathVariable Long id) {
        service.delete(id);
        return Result.success(null);
    }

    @PostMapping("/{id}/toggle")
    public Result<HermesErrorPattern> toggle(@PathVariable Long id) {
        return Result.success(service.toggle(id));
    }

    @PostMapping("/reset")
    public Result<Void> reset() {
        service.resetToDefaults();
        return Result.success(null);
    }

    @Data
    public static class PatternReq {
        private String name;
        private String pattern;
        private String description;
        private Boolean enabled;
    }
}
