import {
  Body,
  Controller,
  Get,
  Param,
  ParseUUIDPipe,
  Patch,
  UseGuards,
} from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';

import { updateServerPropertiesSchema } from '@hexahost/contracts';

import { CurrentUser } from '../../auth/decorators/current-user.decorator';
import { SessionGuard } from '../../auth/guards/session.guard';
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';

import { PropertiesService } from './properties.service';

@ApiTags('servers')
@Controller('servers/:serverId/properties')
@UseGuards(SessionGuard)
export class PropertiesController {
  constructor(private readonly propertiesService: PropertiesService) {}

  @Get()
  @ApiOperation({ summary: 'Get validated server.properties values' })
  @ApiResponse({ status: 200, description: 'Server properties' })
  get(
    @CurrentUser() user: { id: string },
    @Param('serverId', ParseUUIDPipe) serverId: string,
  ) {
    return this.propertiesService.getProperties(user.id, serverId);
  }

  @Patch()
  @ApiOperation({ summary: 'Update server.properties values' })
  @ApiResponse({ status: 200, description: 'Updated server properties' })
  patch(
    @CurrentUser() user: { id: string },
    @Param('serverId', ParseUUIDPipe) serverId: string,
    @Body(new ZodValidationPipe(updateServerPropertiesSchema)) body: unknown,
  ) {
    return this.propertiesService.updateProperties(
      user.id,
      serverId,
      body as Parameters<PropertiesService['updateProperties']>[2],
    );
  }
}
