package com.loremind.infrastructure.web.controller;

import com.loremind.application.campaigncontext.ArcService;
import com.loremind.domain.campaigncontext.Arc;
import com.loremind.infrastructure.web.dto.campaigncontext.ArcDTO;
import com.loremind.infrastructure.web.mapper.ArcMapper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * REST Controller pour le contexte Arc.
 */
@RestController
@RequestMapping("/api/arcs")
public class ArcController {

    private final ArcService arcService;
    private final ArcMapper arcMapper;

    public ArcController(ArcService arcService, ArcMapper arcMapper) {
        this.arcService = arcService;
        this.arcMapper = arcMapper;
    }

    @PostMapping
    public ResponseEntity<ArcDTO> createArc(@RequestBody ArcDTO arcDTO) {
        // Surcharge "Arc complet" pour propager le champ type (LINEAR/HUB) à la création.
        Arc createdArc = arcService.createArc(arcMapper.toDomain(arcDTO));
        return ResponseEntity.ok(arcMapper.toDTO(createdArc));
    }

    @GetMapping("/{id}")
    public ResponseEntity<ArcDTO> getArcById(@PathVariable String id) {
        return arcService.getArcById(id)
                .map(arc -> ResponseEntity.ok(arcMapper.toDTO(arc)))
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping
    public ResponseEntity<List<ArcDTO>> getAllArcs(
            @RequestParam(value = "campaignId", required = false) String campaignId) {
        List<Arc> arcs = (campaignId != null && !campaignId.isBlank())
                ? arcService.getArcsByCampaignId(campaignId)
                : arcService.getAllArcs();
        List<ArcDTO> arcDTOs = arcs.stream()
                .map(arcMapper::toDTO)
                .collect(Collectors.toList());
        return ResponseEntity.ok(arcDTOs);
    }

    @PutMapping("/{id}")
    public ResponseEntity<ArcDTO> updateArc(@PathVariable String id, @RequestBody ArcDTO arcDTO) {
        Arc updatedArc = arcService.updateArc(id, arcMapper.toDomain(arcDTO));
        return ResponseEntity.ok(arcMapper.toDTO(updatedArc));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteArc(@PathVariable String id) {
        arcService.deleteArc(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/{id}/deletion-impact")
    public ResponseEntity<ArcService.DeletionImpact> getDeletionImpact(@PathVariable String id) {
        if (!arcService.arcExists(id)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(arcService.getDeletionImpact(id));
    }

    /** Réordonne les arcs (drag-and-drop) : order = position dans orderedIds. */
    @PutMapping("/reorder")
    public ResponseEntity<Void> reorder(@RequestBody ReorderRequest req) {
        arcService.reorderArcs(req.orderedIds());
        return ResponseEntity.noContent().build();
    }

    public record ReorderRequest(List<String> orderedIds) {}
}
