// packages/backend/src/modules/netaclaw/browser-daemon/controller/admin/state.ts
import { Provide, Inject, Controller, Post, Body } from '@midwayjs/core';
import { CoolTag, CoolUrlTag, TagTypes } from '@cool-midway/core';
import { BrowserDaemonService } from '../../service/daemon.service.js';

@Provide()
@CoolUrlTag()
@Controller('/admin/browser-daemon')
export class AdminBrowserDaemonStateController {
  @Inject() daemonService: BrowserDaemonService;

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/cookie-list')
  async cookieList(@Body() dto: { sessionName: string; domain?: string }) {
    return { code: 1000, data: await this.daemonService.getCookies(dto.sessionName, dto.domain) };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/state-save')
  async stateSave(@Body() dto: { sessionName: string; filePath?: string }) {
    const saved = await this.daemonService.saveState(dto.sessionName, dto.filePath);
    return { code: 1000, data: { saved } };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/state-load')
  async stateLoad(@Body() dto: { sessionName: string; filePath: string }) {
    await this.daemonService.loadState(dto.sessionName, dto.filePath);
    return { code: 1000, data: { loaded: dto.filePath } };
  }
}
