package com.erp.warehouse.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.erp.common.core.model.ApiResponse;
import com.erp.warehouse.dto.StockAlertDTO.*;
import com.erp.warehouse.entity.StockAlertConfig;
import com.erp.warehouse.entity.StockAlertNotification;
import com.erp.warehouse.service.StockAlertService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 库存预警控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/stock-alert")
@RequiredArgsConstructor
public class StockAlertController {

    private final StockAlertService stockAlertService;

    /**
     * 预警列表（分页）
     * GET /api/stock-alert/list
     */
    @GetMapping("/list")
    public ApiResponse<IPage<AlertItem>> listAlerts(
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Long tenantId,
            @RequestParam(required = false) Long warehouseId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String alertLevel,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int limit) {

        IPage<AlertItem> result = stockAlertService.listAlerts(tenantId, warehouseId, keyword, alertLevel, page, limit);
        return ApiResponse.success(result);
    }

    /**
     * 预警摘要
     * GET /api/stock-alert/summary
     */
    @GetMapping("/summary")
    public ApiResponse<AlertSummary> getSummary(
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Long tenantId) {
        AlertSummary summary = stockAlertService.getSummary(tenantId);
        return ApiResponse.success(summary);
    }

    /**
     * 手动触发预警检查并发送通知
     * POST /api/stock-alert/trigger
     */
    @PostMapping("/trigger")
    public ApiResponse<AlertTriggerResult> trigger(
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Long tenantId) {
        AlertTriggerResult result = stockAlertService.triggerAlerts(tenantId);
        String msg = result.getChecked() == 0 ? "库存正常，无预警商品"
                : String.format("检测到 %d 条预警，已发送 %d 个通知渠道", result.getChecked(), result.getTriggered());
        return ApiResponse.success(result, msg);
    }

    // ==================== 预警设置 ====================

    /**
     * 获取预警设置
     * GET /api/stock-alert/settings
     */
    @GetMapping("/settings")
    public ApiResponse<AlertSettings> getSettings(
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Long tenantId) {
        AlertSettings settings = stockAlertService.getSettings(tenantId);
        return ApiResponse.success(settings);
    }

    /**
     * 更新预警设置
     * PUT /api/stock-alert/settings
     */
    @PutMapping("/settings")
    public ApiResponse<Void> updateSettings(
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Long tenantId,
            @RequestBody AlertSettings settings) {
        stockAlertService.updateSettings(tenantId, settings);
        return ApiResponse.success("预警设置已更新", null);
    }

    // ==================== 通知配置 ====================

    /**
     * 获取通知配置
     * GET /api/stock-alert/notifications
     */
    @GetMapping("/notifications")
    public ApiResponse<AlertNotification> getNotifications(
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Long tenantId) {
        AlertNotification notification = stockAlertService.getNotifications(tenantId);
        return ApiResponse.success(notification);
    }

    /**
     * 更新通知配置
     * PUT /api/stock-alert/notifications
     */
    @PutMapping("/notifications")
    public ApiResponse<Void> updateNotifications(
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Long tenantId,
            @RequestBody AlertNotification notification) {
        stockAlertService.updateNotifications(tenantId, notification);
        return ApiResponse.success("通知配置已更新", null);
    }
}
