import { Controller, HttpCode, HttpStatus, Inject, Post, UseGuards } from '@nestjs/common';
import type { SystemReloadPayload } from '@mosaicstack/types';
import { AdminGuard } from '../admin/admin.guard.js';
import { ChatGateway } from '../chat/chat.gateway.js';
import { ReloadService } from './reload.service.js';

@Controller('api/admin')
@UseGuards(AdminGuard)
export class ReloadController {
  constructor(
    @Inject(ReloadService) private readonly reloadService: ReloadService,
    @Inject(ChatGateway) private readonly chatGateway: ChatGateway,
  ) {}

  @Post('reload')
  @HttpCode(HttpStatus.OK)
  async triggerReload(): Promise<SystemReloadPayload> {
    const result = await this.reloadService.reload('rest');
    this.chatGateway.broadcastReload(result);
    return result;
  }
}
