import {
  BadRequestException,
  Body,
  Controller,
  Get,
  Param,
  ParseIntPipe,
  Patch,
  Post,
  Query,
} from '@nestjs/common';
import {
  createViharSchema,
  updateViharSchema,
  cancelViharSchema,
  closeViharManualSchema,
  listViharsQuerySchema,
  type CreateViharInput,
  type ListViharsQuery,
  type AuthUser,
} from '@vihar/shared';
import { ViharsService } from './vihars.service';
import { WhatsappService } from './whatsapp.service';
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';
import { CurrentUser, RequiresCaptain } from '../../common/decorators/auth.decorators';

@Controller('vihars')
export class ViharsController {
  constructor(
    private readonly vihars: ViharsService,
    private readonly whatsapp: WhatsappService,
  ) {}

  @Get()
  list(
    @CurrentUser() user: AuthUser,
    @Query(new ZodValidationPipe(listViharsQuerySchema)) query: ListViharsQuery,
  ) {
    if (!user.cityId) throw new BadRequestException('No city scope');
    return this.vihars.list(user.cityId, query, query.mine ? user.userId : undefined);
  }

  @Get(':id')
  get(@CurrentUser() user: AuthUser, @Param('id', ParseIntPipe) id: number) {
    if (!user.cityId) throw new BadRequestException('No city scope');
    return this.vihars.getById(user.cityId, id);
  }

  @Post()
  @RequiresCaptain()
  create(
    @CurrentUser() user: AuthUser,
    @Body(new ZodValidationPipe(createViharSchema)) input: CreateViharInput,
  ) {
    if (!user.cityId) throw new BadRequestException('No city scope');
    return this.vihars.create(user.cityId, user.userId, input);
  }

  @Patch(':id')
  @RequiresCaptain()
  update(
    @CurrentUser() user: AuthUser,
    @Param('id', ParseIntPipe) id: number,
    @Body(new ZodValidationPipe(updateViharSchema)) _input: unknown,
  ) {
    if (!user.cityId) throw new BadRequestException('No city scope');
    // TODO(claude-code): implement partial update with audit logging.
    return { message: 'Vihar update not yet implemented', viharId: id };
  }

  @Post(':id/cancel')
  @RequiresCaptain()
  cancel(
    @CurrentUser() user: AuthUser,
    @Param('id', ParseIntPipe) id: number,
    @Body(new ZodValidationPipe(cancelViharSchema)) input: { reason: string },
  ) {
    if (!user.cityId) throw new BadRequestException('No city scope');
    return this.vihars.cancel(user.cityId, id, user.userId, input.reason);
  }

  @Post(':id/close-manual')
  @RequiresCaptain()
  closeManual(
    @CurrentUser() user: AuthUser,
    @Param('id', ParseIntPipe) id: number,
    @Body(new ZodValidationPipe(closeViharManualSchema)) input: ReturnType<typeof closeViharManualSchema.parse>,
  ) {
    if (!user.cityId) throw new BadRequestException('No city scope');
    return this.vihars.closeManual(user.cityId, id, user.userId, input);
  }

  @Post(':id/reopen')
  @RequiresCaptain()
  reopen(@CurrentUser() user: AuthUser, @Param('id', ParseIntPipe) id: number) {
    if (!user.cityId) throw new BadRequestException('No city scope');
    return this.vihars.reopen(user.cityId, id, user.userId);
  }

  @Get(':id/whatsapp-message')
  whatsappMessage(@CurrentUser() user: AuthUser, @Param('id', ParseIntPipe) id: number) {
    if (!user.cityId) throw new BadRequestException('No city scope');
    return this.whatsapp.buildMessageForVihar(user.cityId, id);
  }
}
