import { Body, Controller, Get, Param, Post, Query, UseGuards } from '@nestjs/common';
import { SkipThrottle } from '@nestjs/throttler';

import type { EdgeResolveResponse, EdgeStartResponse } from '@hexahost/contracts';
import { edgeStartRequestSchema } from '@hexahost/contracts';

import { EdgeInternalGuard } from './edge-internal.guard';
import { EdgeService } from './edge.service';

@Controller('internal/edge/v1')
@UseGuards(EdgeInternalGuard)
@SkipThrottle()
export class EdgeController {
  constructor(private readonly edgeService: EdgeService) {}

  @Get('resolve/:slug')
  resolve(
    @Param('slug') slug: string,
    @Query('clientIp') clientIp?: string,
  ): Promise<EdgeResolveResponse> {
    return this.edgeService.resolveSlug(slug, clientIp);
  }

  @Post('start/:slug')
  start(
    @Param('slug') slug: string,
    @Body() body: unknown,
  ): Promise<EdgeStartResponse> {
    const parsed = edgeStartRequestSchema.parse(body);
    return this.edgeService.requestJoinStart(slug, parsed.clientIp);
  }
}
