import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  Query,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';
import { ActivitiesService } from './activities.service';
import { CreateActivityDto } from './dto/create-activity.dto';
import { UpdateActivityDto } from './dto/update-activity.dto';
import { QueryActivitiesDto } from './dto/query-activities.dto';
import { CurrentUser, JwtPayload } from '../common/decorators';
import { TenantGuard } from '../auth/guards/tenant.guard';
import {
  paginatedResponse,
  singleResponse,
} from '../common/dto/pagination.dto';

@ApiTags('Activities')
@ApiBearerAuth('access-token')
@UseGuards(TenantGuard)
@Controller('activities')
export class ActivitiesController {
  constructor(private readonly activitiesService: ActivitiesService) {}

  @Post()
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({ summary: 'Aktivitaet erstellen' })
  async create(
    @CurrentUser() user: JwtPayload,
    @Body() dto: CreateActivityDto,
  ) {
    const activity = await this.activitiesService.create(
      user.tenantId!,
      user.sub,
      dto,
    );
    return singleResponse(activity);
  }

  @Get()
  @ApiOperation({ summary: 'Aktivitaeten auflisten (paginiert, filterbar)' })
  async findAll(
    @CurrentUser() user: JwtPayload,
    @Query() query: QueryActivitiesDto,
  ) {
    const result = await this.activitiesService.findAll(
      user.tenantId!,
      query,
    );
    return paginatedResponse(
      result.data,
      result.total,
      result.page,
      result.pageSize,
    );
  }

  @Get('open-tasks')
  @ApiOperation({ summary: 'Offene Aufgaben (TASK + FOLLOWUP) abrufen' })
  async findOpenTasks(@CurrentUser() user: JwtPayload) {
    const tasks = await this.activitiesService.findOpenTasks(user.tenantId!);
    return { success: true, data: tasks, meta: { count: tasks.length } };
  }

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

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

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