package com.walkguide.controller;

import com.walkguide.dto.ApiResponse;
import com.walkguide.dto.request.*;
import com.walkguide.dto.response.PairingStatusResponse;
import com.walkguide.security.SecurityHelper;
import com.walkguide.service.PairingService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/shared/pairing")
@RequiredArgsConstructor
public class PairingController {

    private final PairingService pairingService;

    @PostMapping("/invite")
    public ResponseEntity<ApiResponse<PairingStatusResponse>> invite(
            @RequestBody InviteUserRequest req) {
        Long guardianId = SecurityHelper.getCurrentUserId();
        return ResponseEntity.ok(ApiResponse.ok(
                pairingService.inviteUser(guardianId, req.getUniqueUserId()),
                "Undangan dikirim ke user"));
    }

    @PostMapping("/respond")
    public ResponseEntity<ApiResponse<PairingStatusResponse>> respond(
            @RequestBody PairingResponseRequest req) {
        Long userId = SecurityHelper.getCurrentUserId();
        return ResponseEntity.ok(ApiResponse.ok(
                pairingService.respondToPairing(userId, req.getPairingId(), req.isAccept()),
                req.isAccept() ? "Pairing diterima" : "Pairing ditolak"));
    }

    @DeleteMapping("/unpair")
    public ResponseEntity<ApiResponse<Void>> unpair() {
        pairingService.unpair(SecurityHelper.getCurrentUserId());
        return ResponseEntity.ok(ApiResponse.ok(null, "Pairing diakhiri"));
    }

    @GetMapping("/status")
    public ResponseEntity<ApiResponse<PairingStatusResponse>> status() {
        Long userId = SecurityHelper.getCurrentUserId();
        String role = SecurityHelper.getCurrentUserRole();
        return ResponseEntity.ok(ApiResponse.ok(
                pairingService.getStatus(userId, role), "Status pairing"));
    }
}
