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

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

    private void check(User user) {
        mallService.requireRole(user, UserRole.ADMIN);
    }

    @GetMapping("/overview")
    public ApiResponse<Map<String, Object>> overview(HttpServletRequest request) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminOverview());
    }

    @GetMapping("/orders")
    public ApiResponse<List<Orders>> orders(HttpServletRequest request) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminOrders());
    }

    @PutMapping("/orders/{id}")
    public ApiResponse<Orders> updateOrder(HttpServletRequest request, @PathVariable Long id, @RequestBody Map<String, String> body) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminUpdateOrder(id, body.get("status"), body.getOrDefault("logisticsInfo", "")));
    }

    @GetMapping("/orders/risk")
    public ApiResponse<List<Map<String, Object>>> riskOrders(HttpServletRequest request) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminRiskOrders());
    }

    @PutMapping("/orders/{id}/refund-audit")
    public ApiResponse<Orders> auditRefund(HttpServletRequest request, @PathVariable Long id, @RequestBody Map<String, Object> body) {
        check(AuthContext.getUser(request));
        boolean approve = Boolean.parseBoolean(String.valueOf(body.getOrDefault("approve", false)));
        String remark = String.valueOf(body.getOrDefault("remark", ""));
        return ApiResponse.ok(mallService.adminAuditRefund(id, approve, remark));
    }

    @PutMapping("/orders/{id}/ship-audit")
    public ApiResponse<Orders> auditShipment(HttpServletRequest request, @PathVariable Long id, @RequestBody Map<String, Object> body) {
        check(AuthContext.getUser(request));
        boolean approve = Boolean.parseBoolean(String.valueOf(body.getOrDefault("approve", true)));
        String remark = String.valueOf(body.getOrDefault("remark", ""));
        return ApiResponse.ok(mallService.adminAuditShipment(id, approve, remark));
    }

    @GetMapping("/merchant-applications")
    public ApiResponse<List<Map<String, Object>>> merchantApplications(HttpServletRequest request) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminMerchantApplications());
    }

    @PutMapping("/merchant-applications/{id}")
    public ApiResponse<MerchantApplication> auditMerchant(HttpServletRequest request, @PathVariable Long id, @RequestBody Map<String, String> body) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminAuditApplication(id, body.get("status"), body.getOrDefault("remark", "")));
    }

    @GetMapping("/users")
    public ApiResponse<List<User>> users(HttpServletRequest request) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminUsers());
    }

    @PostMapping("/users")
    public ApiResponse<User> saveUser(HttpServletRequest request, @RequestBody User user) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminSaveUser(user));
    }

    @DeleteMapping("/users/{id}")
    public ApiResponse<Void> deleteUser(HttpServletRequest request, @PathVariable Long id) {
        check(AuthContext.getUser(request));
        mallService.adminDeleteUser(id);
        return ApiResponse.ok();
    }

    @GetMapping("/banners")
    public ApiResponse<List<Banner>> banners(HttpServletRequest request) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminBanners());
    }

    @PostMapping("/banners")
    public ApiResponse<Banner> saveBanner(HttpServletRequest request, @RequestBody Banner banner) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminSaveBanner(banner));
    }

    @DeleteMapping("/banners/{id}")
    public ApiResponse<Void> deleteBanner(HttpServletRequest request, @PathVariable Long id) {
        check(AuthContext.getUser(request));
        mallService.adminDeleteBanner(id);
        return ApiResponse.ok();
    }

    @GetMapping("/products")
    public ApiResponse<List<Product>> products(HttpServletRequest request) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminProducts());
    }

    @GetMapping("/products/views")
    public ApiResponse<List<Map<String, Object>>> productViews(HttpServletRequest request) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminProductViews());
    }

    @PostMapping("/products")
    public ApiResponse<Product> saveProduct(HttpServletRequest request, @RequestBody Product product) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminSaveProduct(product));
    }

    @PutMapping("/products/{id}/approve")
    public ApiResponse<Product> approveProduct(HttpServletRequest request, @PathVariable Long id, @RequestBody Map<String, Object> body) {
        check(AuthContext.getUser(request));
        boolean approved = Boolean.parseBoolean(String.valueOf(body.getOrDefault("approved", true)));
        return ApiResponse.ok(mallService.adminApproveProduct(id, approved));
    }

    @DeleteMapping("/products/{id}")
    public ApiResponse<Void> deleteProduct(HttpServletRequest request, @PathVariable Long id) {
        check(AuthContext.getUser(request));
        mallService.adminDeleteProduct(id);
        return ApiResponse.ok();
    }

    @GetMapping("/reviews")
    public ApiResponse<List<Map<String, Object>>> reviews(HttpServletRequest request) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminReviews());
    }

    @GetMapping("/logistics")
    public ApiResponse<List<Map<String, Object>>> logistics(HttpServletRequest request) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminLogistics());
    }

    @GetMapping("/inventory")
    public ApiResponse<List<Map<String, Object>>> inventory(HttpServletRequest request) {
        check(AuthContext.getUser(request));
        return ApiResponse.ok(mallService.adminInventory());
    }
}
