// packages/backend/src/modules/netaclaw/browser-daemon/controller/admin/interaction.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 AdminBrowserDaemonInteractionController {
  @Inject() daemonService: BrowserDaemonService;

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/click')
  async click(@Body() dto: { sessionName: string; ref: string; mode?: 'full' | 'fast' | 'off' }) {
    await this.daemonService.click(dto.sessionName, dto.ref, dto.mode ?? 'full');
    return { code: 1000, data: { clicked: dto.ref } };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/hover')
  async hover(@Body() dto: { sessionName: string; ref: string; mode?: 'full' | 'fast' | 'off' }) {
    await this.daemonService.hover(dto.sessionName, dto.ref, dto.mode ?? 'full');
    return { code: 1000, data: { hovered: dto.ref } };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/fill')
  async fill(@Body() dto: { sessionName: string; ref: string; value: string; mode?: 'full' | 'fast' | 'off' }) {
    await this.daemonService.fill(dto.sessionName, dto.ref, dto.value, dto.mode ?? 'full');
    return { code: 1000, data: { filled: dto.ref } };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/type')
  async type(@Body() dto: { sessionName: string; text: string; mode?: 'full' | 'fast' | 'off' }) {
    await this.daemonService.type(dto.sessionName, dto.text, dto.mode ?? 'full');
    return { code: 1000, data: { typed: dto.text.length } };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/select-option')
  async selectOption(@Body() dto: { sessionName: string; ref: string; value: string | string[] }) {
    const selected = await this.daemonService.selectOption(dto.sessionName, dto.ref, dto.value);
    return { code: 1000, data: { selected } };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/check')
  async check(@Body() dto: { sessionName: string; ref: string; checked?: boolean }) {
    await this.daemonService.check(dto.sessionName, dto.ref, dto.checked ?? true);
    return { code: 1000, data: { checked: dto.checked ?? true } };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/press-key')
  async pressKey(@Body() dto: { sessionName: string; key: string }) {
    await this.daemonService.pressKey(dto.sessionName, dto.key);
    return { code: 1000, data: { pressed: dto.key } };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/scroll')
  async scroll(@Body() dto: { sessionName: string; deltaY: number; mode?: 'full' | 'fast' | 'off' }) {
    await this.daemonService.scroll(dto.sessionName, dto.deltaY, dto.mode ?? 'full');
    return { code: 1000, data: { scrolled: dto.deltaY } };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/wait-for')
  async waitFor(@Body() dto: { sessionName: string; ref?: string; state?: 'visible' | 'hidden' | 'attached' | 'detached'; loadState?: 'load' | 'domcontentloaded' | 'networkidle'; timeout?: number }) {
    await this.daemonService.waitFor(dto.sessionName, dto);
    return { code: 1000, data: { ok: true } };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/screenshot')
  async screenshot(@Body() dto: { sessionName: string; fullPage?: boolean }) {
    const base64 = await this.daemonService.screenshot(dto.sessionName, dto);
    return { code: 1000, data: { base64 } };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/eval')
  async evalScript(@Body() dto: { sessionName: string; script: string }) {
    const result = await this.daemonService.evalScript(dto.sessionName, dto.script);
    return { code: 1000, data: { result } };
  }
}
