package com.smyhub.store.mengyastorebackendjava.controller;

import com.smyhub.store.mengyastorebackendjava.dto.response.ApiResponse;
import com.smyhub.store.mengyastorebackendjava.security.CurrentUserHolder;
import com.smyhub.store.mengyastorebackendjava.service.WishlistService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/wishlist")
@RequiredArgsConstructor
public class WishlistController {

    private final WishlistService wishlistService;

    @GetMapping
    public ResponseEntity<ApiResponse<Map<String, List<String>>>> getWishlist() {
        String account = CurrentUserHolder.get().getAccount();
        List<String> items = wishlistService.getWishlistIds(account);
        return ResponseEntity.ok(ApiResponse.ok(Map.of("items", items)));
    }

    @PostMapping
    public ResponseEntity<ApiResponse<Map<String, List<String>>>> addToWishlist(
            @RequestBody Map<String, String> body) {
        String productId = body.get("productId");
        String account = CurrentUserHolder.get().getAccount();
        wishlistService.addToWishlist(account, productId);
        List<String> items = wishlistService.getWishlistIds(account);
        return ResponseEntity.ok(ApiResponse.ok(Map.of("items", items)));
    }

    @DeleteMapping("/{productId}")
    public ResponseEntity<ApiResponse<Map<String, List<String>>>> removeFromWishlist(
            @PathVariable String productId) {
        String account = CurrentUserHolder.get().getAccount();
        wishlistService.removeFromWishlist(account, productId);
        List<String> items = wishlistService.getWishlistIds(account);
        return ResponseEntity.ok(ApiResponse.ok(Map.of("items", items)));
    }
}
