package com.petshop.backend.controller;

import com.petshop.backend.dto.cart.*;
import com.petshop.backend.service.CartService;
import com.petshop.backend.util.AuthenticationHelper;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/cart")
public class CartController {

    private final CartService cartService;

    public CartController(CartService cartService) {
        this.cartService = cartService;
    }

    @GetMapping
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<CartResponse> getActiveCart(@RequestParam Long storeId) {
        Long userId = AuthenticationHelper.getAuthenticatedUserId();
        CartResponse cart = cartService.getActiveCart(userId, storeId);

        if (cart == null) {

            return ResponseEntity.noContent().build();
        }

        return ResponseEntity.ok(cart);
    }

    @PostMapping("/add")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<CartResponse> addItem(@Valid @RequestBody AddToCartRequest request) {
        Long userId = AuthenticationHelper.getAuthenticatedUserId();

        return ResponseEntity.ok(cartService.addItem(userId, request));
    }

    @PutMapping("/update")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<CartResponse> updateItem(@Valid @RequestBody UpdateCartItemRequest request) {
        Long userId = AuthenticationHelper.getAuthenticatedUserId();

        return ResponseEntity.ok(cartService.updateItem(userId, request));
    }

    @DeleteMapping("/remove/{cartItemId}")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<CartResponse> removeItem(@PathVariable Long cartItemId) {
        Long userId = AuthenticationHelper.getAuthenticatedUserId();

        return ResponseEntity.ok(cartService.removeItem(userId, cartItemId));
    }

    @DeleteMapping("/clear")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<Void> clearCart(@RequestParam Long storeId) {
        Long userId = AuthenticationHelper.getAuthenticatedUserId();
        cartService.clearCart(userId, storeId);

        return ResponseEntity.noContent().build();
    }

    @PostMapping("/apply-coupon")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<CartResponse> applyCoupon(
            @RequestParam Long storeId,
            @Valid @RequestBody ApplyCouponRequest request) {
        Long userId = AuthenticationHelper.getAuthenticatedUserId();

        return ResponseEntity.ok(cartService.applyCoupon(userId, storeId, request.getCouponCode()));
    }

    @PostMapping("/apply-points")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<CartResponse> applyPoints(
            @RequestParam Long storeId,
            @RequestParam Boolean useLoyaltyPoints) {
        Long userId = AuthenticationHelper.getAuthenticatedUserId();

        return ResponseEntity.ok(cartService.applyPoints(userId, storeId, useLoyaltyPoints));
    }

    @DeleteMapping("/coupon")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<CartResponse> removeCoupon(@RequestParam Long storeId) {
        Long userId = AuthenticationHelper.getAuthenticatedUserId();

        return ResponseEntity.ok(cartService.removeCoupon(userId, storeId));
    }

    @PostMapping("/checkout")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<CheckoutResponse> checkout(@Valid @RequestBody CheckoutRequest request) {
        Long userId = AuthenticationHelper.getAuthenticatedUserId();

        return ResponseEntity.ok(cartService.checkout(userId, request.getStoreId()));
    }

    @PostMapping("/checkout/complete")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<Void> completeCheckout(@RequestParam String paymentIntentId) {
        Long userId = AuthenticationHelper.getAuthenticatedUserId();
        cartService.completeCheckout(userId, paymentIntentId);

        return ResponseEntity.noContent().build();
    }

    @PostMapping("/checkout/cancel")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<Void> cancelCheckout(@RequestParam Long storeId) {
        Long userId = AuthenticationHelper.getAuthenticatedUserId();
        cartService.cancelCheckout(userId, storeId);

        return ResponseEntity.noContent().build();
    }
}
