import 'reflect-metadata';
import { PERMISSION_KEY } from '../auth/decorators/permission.decorator';
import { ArchiveController } from './archive.controller';

describe('ArchiveController purge routes', () => {
  it('requires archive:purge on permanent delete routes', () => {
    expect(
      Reflect.getMetadata(PERMISSION_KEY, ArchiveController.prototype.purgeEnrollment),
    ).toEqual(['archive:purge']);
    expect(
      Reflect.getMetadata(PERMISSION_KEY, ArchiveController.prototype.purgeExamScore),
    ).toEqual(['archive:purge']);
    expect(
      Reflect.getMetadata(PERMISSION_KEY, ArchiveController.prototype.purgeLearningRecord),
    ).toEqual(['archive:purge']);
    expect(
      Reflect.getMetadata(PERMISSION_KEY, ArchiveController.prototype.purgeAttachment),
    ).toEqual(['archive:purge']);
  });

  it('writes permanent delete audit logs for sub-records', async () => {
    const archiveService = {
      purgeEnrollment: jest.fn().mockResolvedValue({ message: '已永久删除报名记录（不可恢复）' }),
    };
    const log = jest.fn().mockResolvedValue(undefined);
    const controller = new ArchiveController(
      archiveService as never,
      { log } as never,
      {} as never,
    );
    const req = { user: { id: 1, username: 'admin' }, ip: '127.0.0.1', headers: {} };
    await controller.purgeEnrollment(1, req);
    expect(archiveService.purgeEnrollment).toHaveBeenCalledWith(1);
    expect(log).toHaveBeenCalledWith(
      expect.objectContaining({ module: '学生档案', action: '永久删除报名记录', targetId: 1 }),
    );
  });
});
