package com.maternalmall.controller;

import com.maternalmall.common.ApiResponse;
import com.maternalmall.config.AuthContext;
import com.maternalmall.domain.*;
import com.maternalmall.service.MallService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/customer")
public class CustomerController {
    private final MallService mallService;

    public CustomerController(MallService mallService) {
        this.mallService = mallService;
    }

    @GetMapping("/cart")
    public ApiResponse<List<CartItem>> cart(HttpServletRequest request) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.cartList(user.getId()));
    }

    @GetMapping("/cart/views")
    public ApiResponse<List<Map<String, Object>>> cartViews(HttpServletRequest request) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.cartViews(user.getId()));
    }

    @PostMapping("/cart")
    public ApiResponse<CartItem> addCart(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.addCart(user.getId(), Long.valueOf(body.get("productId").toString()), Integer.valueOf(body.get("quantity").toString())));
    }

    @DeleteMapping("/cart/{productId}")
    public ApiResponse<Void> removeCart(HttpServletRequest request, @PathVariable Long productId) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        mallService.removeCart(user.getId(), productId);
        return ApiResponse.ok();
    }

    @GetMapping("/favorites")
    public ApiResponse<List<Favorite>> favorites(HttpServletRequest request) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.favoriteList(user.getId()));
    }

    @GetMapping("/favorites/views")
    public ApiResponse<List<Map<String, Object>>> favoriteViews(HttpServletRequest request) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.favoriteViews(user.getId()));
    }

    @PostMapping("/favorites")
    public ApiResponse<Favorite> addFavorite(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.addFavorite(user.getId(), Long.valueOf(body.get("productId").toString())));
    }

    @DeleteMapping("/favorites/{productId}")
    public ApiResponse<Void> removeFavorite(HttpServletRequest request, @PathVariable Long productId) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        mallService.removeFavorite(user.getId(), productId);
        return ApiResponse.ok();
    }

    @PostMapping("/orders/checkout")
    public ApiResponse<Orders> checkout(HttpServletRequest request, @RequestBody Map<String, String> body) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.checkout(user.getId(), body.get("address")));
    }

    @PostMapping("/orders/buy-now")
    public ApiResponse<Orders> buyNow(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        Long productId = Long.valueOf(body.get("productId").toString());
        Integer quantity = Integer.valueOf(String.valueOf(body.getOrDefault("quantity", 1)));
        String address = String.valueOf(body.getOrDefault("address", ""));
        return ApiResponse.ok(mallService.buyNow(user.getId(), productId, quantity, address));
    }

    @GetMapping("/orders")
    public ApiResponse<List<Orders>> orders(HttpServletRequest request) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.customerOrders(user.getId()));
    }

    @GetMapping("/orders/{orderId}/items")
    public ApiResponse<List<OrderItem>> orderItems(HttpServletRequest request, @PathVariable Long orderId) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.customerOrderItems(user.getId(), orderId));
    }

    @PutMapping("/orders/{orderId}/address")
    public ApiResponse<Orders> updateAddress(HttpServletRequest request, @PathVariable Long orderId, @RequestBody Map<String, String> body) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.customerUpdateAddress(user.getId(), orderId, body.get("address")));
    }

    @PutMapping("/orders/{orderId}/refund")
    public ApiResponse<Orders> refund(HttpServletRequest request, @PathVariable Long orderId, @RequestBody Map<String, String> body) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.customerRefund(user.getId(), orderId, body.get("reason")));
    }

    @DeleteMapping("/orders/{orderId}")
    public ApiResponse<Void> deleteOrder(HttpServletRequest request, @PathVariable Long orderId) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        mallService.customerDeleteOrder(user.getId(), orderId);
        return ApiResponse.ok();
    }

    @GetMapping("/orders/{orderId}/logistics")
    public ApiResponse<List<LogisticsRecord>> logistics(HttpServletRequest request, @PathVariable Long orderId) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.logisticsOfOrder(user.getId(), orderId));
    }

    @PostMapping("/reviews")
    public ApiResponse<Review> review(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.CUSTOMER);
        return ApiResponse.ok(mallService.addReview(
                user.getId(),
                Long.valueOf(body.get("orderId").toString()),
                Long.valueOf(body.get("productId").toString()),
                Integer.valueOf(body.get("rating").toString()),
                body.get("content").toString()));
    }

    @PostMapping("/merchant-applications")
    public ApiResponse<MerchantApplication> applyMerchant(HttpServletRequest request, @RequestBody Map<String, String> body) {
        User user = AuthContext.getUser(request);
        return ApiResponse.ok(mallService.applyMerchant(user.getId(), body.getOrDefault("qualification", "")));
    }
}
