import { Provide, Inject, Post, Body } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { NetaClawMemoryTypeEntity } from '../../entity/memory_type.js';
import { MemoryTypeService } from '../../service/memory_type.js';

@Provide()
@CoolController({
  api: ['add', 'update', 'info', 'list', 'page'],
  entity: NetaClawMemoryTypeEntity,
  service: MemoryTypeService,
  pageQueryOp: {
    keyWordLikeFields: ['key', 'name'],
    fieldEq: ['isSystem'],
    addOrderBy: { isSystem: 'DESC', createTime: 'ASC' },
  },
})
export class AdminNetaClawMemoryTypeController extends BaseController {
  @Inject()
  memoryTypeService: MemoryTypeService;

  @Post('/delete', { summary: '删除类型（系统内置不可删）' })
  async typeDelete(@Body() body: { ids: number[] }) {
    const ids = Array.isArray(body.ids) ? [...new Set(body.ids)] : [];
    if (!ids.length || ids.some(id => !Number.isInteger(id) || id <= 0)) {
      return this.fail('ids 必须是正整数数组');
    }

    try {
      await this.memoryTypeService.deleteWithCheck(ids);
      return this.ok();
    } catch (e: unknown) {
      return this.fail(e instanceof Error ? e.message : '删除失败');
    }
  }
}
