import {
  BaseController,
  CoolController,
  CoolTag,
  CoolUrlTag,
  TagTypes,
} from '@cool-midway/core';
import { Body, Inject, Post } from '@midwayjs/core';
import { NotificationService } from '../../service/notify';

/**
 * 通知开放接口
 */
@CoolUrlTag()
@CoolController()
export class OpenNotificationNotifyController extends BaseController {
  @Inject()
  notificationService: NotificationService;

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/send', { summary: '发送通知消息' })
  async send(@Body() body: any) {
    try {
      const result = await this.notificationService.send({
        platform: body.platform || 'feishu',
        title: body.title,
        message: body.message,
        messageType: body.messageType || 'text',
        mentionAll: body.mentionAll,
        receiveId: body.receiveId,
        receiveIdType: body.receiveIdType,
        targetUser: body.targetUser,
        appId: body.appId,
        appSecret: body.appSecret,
        sourceType: 'api',
        sourceId: body.sourceId,
        sourceName: body.sourceName || 'open_notification_send',
        metadata: body.metadata,
      });
      return this.ok(result);
    } catch (e) {
      return this.fail(e.message);
    }
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/batchSend', { summary: '批量发送通知消息' })
  async batchSend(@Body() body: any) {
    try {
      const result = await this.notificationService.sendBatch({
        platform: body.platform || 'feishu',
        title: body.title,
        message: body.message,
        messageType: body.messageType || 'text',
        mentionAll: body.mentionAll,
        appId: body.appId,
        appSecret: body.appSecret,
        sourceType: 'api',
        sourceName: body.sourceName || 'open_notification_batch_send',
        metadata: body.metadata,
        targets: body.targets || [],
      });
      return this.ok(result);
    } catch (e) {
      return this.fail(e.message);
    }
  }
}
