package de.oaa.xxx.games.bdsm.controller;

import de.oaa.xxx.games.bdsm.entity.BdsmSetupDraftEntity;
import de.oaa.xxx.games.bdsm.repository.BdsmSetupDraftRepository;
import de.oaa.xxx.user.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/bdsm/setup-draft")
@Transactional
public class BdsmSetupDraftController {

    private final BdsmSetupDraftRepository draftRepository;
    private final UserService userService;

    public BdsmSetupDraftController(BdsmSetupDraftRepository draftRepository, UserService userService) {
        this.draftRepository = draftRepository;
        this.userService = userService;
    }

    record DraftRequest(String setupId, String settingsJson, String setupJson, String gruppenJson) {}

    @GetMapping
    public ResponseEntity<Map<String, Object>> getDraft(
            @RequestParam(required = false) String setupId,
            Principal principal) {
        UUID userId = userService.requireUser(principal).getUserId();
        var lookup = (setupId != null && !setupId.isBlank())
                ? draftRepository.findBySetupId(setupId)
                : draftRepository.findByUserId(userId);
        return lookup
                .map(d -> {
                    Map<String, Object> m = new LinkedHashMap<>();
                    m.put("setupId",      d.getSetupId());
                    m.put("settingsJson", d.getSettingsJson());
                    m.put("setupJson",    d.getSetupJson());
                    m.put("gruppenJson",  d.getGruppenJson());
                    return ResponseEntity.ok(m);
                })
                .orElse(ResponseEntity.noContent().build());
    }

    @PutMapping
    public ResponseEntity<Void> saveDraft(@RequestBody DraftRequest req, Principal principal) {
        UUID userId = userService.requireUser(principal).getUserId();
        BdsmSetupDraftEntity d = draftRepository.findByUserId(userId)
                .orElseGet(() -> { BdsmSetupDraftEntity n = new BdsmSetupDraftEntity(); n.setUserId(userId); return n; });
        if (req.setupId() != null)    d.setSetupId(req.setupId());
        if (req.settingsJson() != null) d.setSettingsJson(req.settingsJson());
        if (req.setupJson() != null)  d.setSetupJson(req.setupJson());
        if (req.gruppenJson() != null) d.setGruppenJson(req.gruppenJson());
        d.setUpdatedAt(LocalDateTime.now());
        draftRepository.save(d);
        return ResponseEntity.accepted().build();
    }

    @DeleteMapping
    public ResponseEntity<Void> deleteDraft(Principal principal) {
        UUID userId = userService.requireUser(principal).getUserId();
        draftRepository.findByUserId(userId).ifPresent(draftRepository::delete);
        return ResponseEntity.accepted().build();
    }
}
