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/merchant")
public class MerchantController {
    private final MallService mallService;

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

    @GetMapping("/overview")
    public ApiResponse<Map<String, Object>> overview(HttpServletRequest request) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.MERCHANT);
        return ApiResponse.ok(mallService.merchantOverview(user.getId()));
    }

    @GetMapping("/products")
    public ApiResponse<List<Product>> products(HttpServletRequest request) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.MERCHANT);
        return ApiResponse.ok(mallService.merchantProducts(user.getId()));
    }

    @PostMapping("/products")
    public ApiResponse<Product> saveProduct(HttpServletRequest request, @RequestBody Product product) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.MERCHANT);
        return ApiResponse.ok(mallService.merchantSaveProduct(user.getId(), product));
    }

    @DeleteMapping("/products/{id}")
    public ApiResponse<Void> deleteProduct(HttpServletRequest request, @PathVariable Long id) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.MERCHANT);
        mallService.merchantDeleteProduct(user.getId(), id);
        return ApiResponse.ok();
    }

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

    @PutMapping("/orders/{id}/ship")
    public ApiResponse<Orders> ship(HttpServletRequest request, @PathVariable Long id, @RequestBody Map<String, String> body) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.MERCHANT);
        return ApiResponse.ok(mallService.merchantShip(user.getId(), id, body.getOrDefault("note", "已发货")));
    }

    @PutMapping("/orders/{id}/refund")
    public ApiResponse<Orders> refund(HttpServletRequest request, @PathVariable Long id, @RequestBody Map<String, Object> body) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.MERCHANT);
        return ApiResponse.ok(mallService.merchantProcessRefund(user.getId(), id, Boolean.parseBoolean(body.get("agree").toString())));
    }

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

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

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

    @DeleteMapping("/inventory/{id}")
    public ApiResponse<Void> deleteInventory(HttpServletRequest request, @PathVariable Long id) {
        User user = AuthContext.getUser(request);
        mallService.requireRole(user, UserRole.MERCHANT);
        mallService.merchantDeleteInventory(user.getId(), id);
        return ApiResponse.ok();
    }
}
