import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  ParseUUIDPipe,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { TenantsService } from './tenants.service';
import { CreateTenantDto } from './dto/create-tenant.dto';
import { UpdateTenantDto } from './dto/update-tenant.dto';
import { AddMemberDto } from './dto/add-member.dto';
import { Roles } from '../../common/decorators/roles.decorator';
import { RolesGuard } from '../../common/guards/roles.guard';

@ApiTags('Mandanten')
@ApiBearerAuth('access-token')
@Controller('tenants')
export class TenantsController {
  constructor(private readonly tenantsService: TenantsService) {}

  /**
   * GET /api/v1/tenants
   * Alle Mandanten auflisten (nur PLATFORM_ADMIN).
   */
  @Get()
  @Roles('PLATFORM_ADMIN')
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: 'Alle Mandanten auflisten (Admin)' })
  async findAll(
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.tenantsService.findAll(
      Number(page) || 1,
      Number(limit) || 20,
    );
  }

  /**
   * GET /api/v1/tenants/:id
   * Mandant nach ID (nur PLATFORM_ADMIN).
   */
  @Get(':id')
  @Roles('PLATFORM_ADMIN')
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: 'Mandant nach ID abrufen (Admin)' })
  async findById(@Param('id', ParseUUIDPipe) id: string) {
    return this.tenantsService.findById(id);
  }

  /**
   * POST /api/v1/tenants
   * Neuen Mandant anlegen (nur PLATFORM_ADMIN).
   */
  @Post()
  @Roles('PLATFORM_ADMIN')
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: 'Neuen Mandanten anlegen (Admin)' })
  async create(@Body() dto: CreateTenantDto) {
    return this.tenantsService.create(dto);
  }

  /**
   * PATCH /api/v1/tenants/:id
   * Mandant aktualisieren (nur PLATFORM_ADMIN).
   */
  @Patch(':id')
  @Roles('PLATFORM_ADMIN')
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: 'Mandant aktualisieren (Admin)' })
  async update(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateTenantDto,
  ) {
    return this.tenantsService.update(id, dto);
  }

  /**
   * POST /api/v1/tenants/:id/members
   * User einem Mandant zuweisen (nur PLATFORM_ADMIN).
   */
  @Post(':id/members')
  @Roles('PLATFORM_ADMIN')
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: 'Benutzer zu Mandant hinzufuegen (Admin)' })
  async addMember(
    @Param('id', ParseUUIDPipe) tenantId: string,
    @Body() dto: AddMemberDto,
  ) {
    return this.tenantsService.addMember(tenantId, dto.userId, dto.role);
  }

  /**
   * DELETE /api/v1/tenants/:id/members/:userId
   * User aus Mandant entfernen (nur PLATFORM_ADMIN).
   */
  @Delete(':id/members/:userId')
  @Roles('PLATFORM_ADMIN')
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: 'Benutzer aus Mandant entfernen (Admin)' })
  async removeMember(
    @Param('id', ParseUUIDPipe) tenantId: string,
    @Param('userId', ParseUUIDPipe) userId: string,
  ) {
    return this.tenantsService.removeMember(tenantId, userId);
  }
}
