package com.erp.notification.controller;

import com.erp.common.core.model.ApiResponse;
import com.erp.notification.client.NotificationClient;
import com.erp.notification.dto.NotificationRequest;
import com.erp.notification.service.NotificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通知控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/notifications")
@RequiredArgsConstructor
public class NotificationController {

    private final NotificationService notificationService;
    private final StringRedisTemplate redisTemplate;

    /**
     * 发送通知（通用接口）
     */
    @PostMapping("/send")
    public ApiResponse<Void> send(@RequestBody NotificationRequest request) {
        boolean success = notificationService.send(request);
        if (success) {
            return ApiResponse.success("发送成功", null);
        } else {
            return ApiResponse.error("发送失败");
        }
    }

    /**
     * 发送钉钉通知
     */
    @PostMapping("/dingtalk")
    public ApiResponse<Void> sendDingTalk(@RequestBody NotificationClient.DingTalkRequest request) {
        notificationService.sendDingTalk(request.getTitle(), request.getContent());
        return ApiResponse.success("发送成功", null);
    }

    /**
     * 发送邮件通知
     */
    @PostMapping("/email")
    public ApiResponse<Void> sendEmail(
            @RequestParam String to,
            @RequestParam String subject,
            @RequestParam String content) {
        notificationService.sendEmail(to, subject, content);
        return ApiResponse.success("发送成功", null);
    }

    /**
     * 发送短信通知
     */
    @PostMapping("/sms")
    public ApiResponse<Void> sendSms(
            @RequestParam String phone,
            @RequestParam String content) {
        notificationService.sendSms(phone, content);
        return ApiResponse.success("发送成功", null);
    }

    /**
     * 发送系统通知
     */
    @PostMapping("/system")
    public ApiResponse<Void> sendSystemNotice(@RequestBody NotificationClient.SystemNoticeRequest request) {
        notificationService.sendSystemNotice(
                request.getTenantId(),
                request.getTitle(),
                request.getContent(),
                request.getBizType(),
                request.getBizId()
        );
        return ApiResponse.success("发送成功", null);
    }

    /**
     * 批量发送通知
     */
    @PostMapping("/batch")
    public ApiResponse<Void> sendBatch(@RequestBody List<NotificationRequest> requests) {
        notificationService.sendBatch(requests);
        return ApiResponse.success("批量发送完成", null);
    }

    /**
     * 获取租户的系统通知列表
     */
    @GetMapping("/tenant/{tenantId}")
    public ApiResponse<List<String>> getTenantNotifications(
            @PathVariable Long tenantId,
            @RequestParam(defaultValue = "20") Integer limit) {
        String key = "notifications:tenant:" + tenantId;
        List<String> notifications = redisTemplate.opsForList().range(key, 0, limit - 1);
        if (notifications == null) {
            notifications = List.of();
        }
        return ApiResponse.success(notifications);
    }
}
