import {
  Body,
  Controller,
  Get,
  Param,
  ParseUUIDPipe,
  Post,
} from '@nestjs/common';
import {
  MeterReadingCreateSchema,
  type MeterReadingCreate,
} from '@siop/shared';
import {
  AuthenticatedUser,
  CurrentUser,
} from '../auth/current-user.decorator';
import { ZodValidationPipe } from '../common/zod-validation.pipe';
import { RequirePermission } from '../permissions/require-permission.decorator';
import { MetersService } from './meters.service';

@Controller('assets/:id')
export class MetersController {
  constructor(private readonly meters: MetersService) {}

  @Get('meters')
  @RequirePermission('METERS', 'view')
  list(@Param('id', ParseUUIDPipe) id: string) {
    return this.meters.list(id);
  }

  @Post('meter-readings')
  @RequirePermission('METERS', 'create')
  addReading(
    @Param('id', ParseUUIDPipe) id: string,
    @Body(new ZodValidationPipe(MeterReadingCreateSchema)) body: MeterReadingCreate,
    @CurrentUser() user: AuthenticatedUser,
  ) {
    return this.meters.addReading(id, body, user);
  }
}
