import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';
import { IndustriesService } from './industries.service';
import { CreateIndustryDto } from './dto/create-industry.dto';
import { UpdateIndustryDto } from './dto/update-industry.dto';
import { CurrentUser, JwtPayload } from '../common/decorators';
import { TenantGuard } from '../auth/guards/tenant.guard';
import { singleResponse } from '../common/dto/pagination.dto';

@ApiTags('Industries')
@ApiBearerAuth('access-token')
@UseGuards(TenantGuard)
@Controller('industries')
export class IndustriesController {
  constructor(private readonly industriesService: IndustriesService) {}

  @Post()
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({ summary: 'Branche erstellen' })
  async create(
    @CurrentUser() user: JwtPayload,
    @Body() dto: CreateIndustryDto,
  ) {
    const industry = await this.industriesService.create(
      user.tenantId!,
      dto,
    );
    return singleResponse(industry);
  }

  @Get()
  @ApiOperation({ summary: 'Alle Branchen auflisten' })
  async findAll(@CurrentUser() user: JwtPayload) {
    const industries = await this.industriesService.findAll(user.tenantId!);
    return { data: industries };
  }

  @Get(':id')
  @ApiOperation({ summary: 'Branche abrufen' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async findOne(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    const industry = await this.industriesService.findOne(
      user.tenantId!,
      id,
    );
    return singleResponse(industry);
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Branche aktualisieren' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async update(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateIndustryDto,
  ) {
    const industry = await this.industriesService.update(
      user.tenantId!,
      id,
      dto,
    );
    return singleResponse(industry);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Branche loeschen' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async remove(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    const industry = await this.industriesService.remove(
      user.tenantId!,
      id,
    );
    return singleResponse(industry);
  }
}
