import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Param,
  Body,
  Query,
  UseGuards,
  HttpCode,
  HttpStatus,
  ParseUUIDPipe,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse, ApiQuery } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { Roles, UserRole } from '../../../common/decorators/roles.decorator';
import { CurrentUser } from '../../../common/decorators/current-user.decorator';
import { ActivitiesService } from '../services/activities.service';
import { CreateActivityDto } from '../dto/create-activity.dto';
import { CompleteActivityDto } from '../dto/complete-activity.dto';

@ApiTags('CRM Activities')
@ApiBearerAuth()
@Controller('crm/activities')
@UseGuards(JwtAuthGuard)
export class ActivitiesController {
  constructor(private readonly activities: ActivitiesService) {}

  @Get('my')
  @ApiOperation({ summary: 'Get current user activities' })
  @ApiQuery({ name: 'cursor', required: false })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'dateFrom', required: false })
  @ApiQuery({ name: 'dateTo', required: false })
  @ApiQuery({ name: 'completed', required: false, type: Boolean })
  @ApiResponse({ status: 200 })
  async getMyActivities(
    @CurrentUser() user: any,
    @Query('cursor') cursor?: string,
    @Query('limit') limit?: string,
    @Query('dateFrom') dateFrom?: string,
    @Query('dateTo') dateTo?: string,
    @Query('completed') completed?: string,
  ) {
    return this.activities.findByUser(
      user.sub,
      {
        dateFrom,
        dateTo,
        completed: completed !== undefined ? completed === 'true' : undefined,
      },
      cursor,
      limit ? +limit : undefined,
    );
  }

  @Get('deal/:dealId')
  @ApiOperation({ summary: 'Get activities for a deal' })
  @ApiQuery({ name: 'cursor', required: false })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiResponse({ status: 200 })
  async getByDeal(
    @Param('dealId', ParseUUIDPipe) dealId: string,
    @Query('cursor') cursor?: string,
    @Query('limit') limit?: string,
  ) {
    return this.activities.findByDeal(dealId, cursor, limit ? +limit : undefined);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get activity by ID' })
  @ApiResponse({ status: 200 })
  async getById(@Param('id', ParseUUIDPipe) id: string) {
    return this.activities.getById(id);
  }

  @Post()
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN, UserRole.MANAGER)
  @ApiOperation({ summary: 'Create an activity' })
  @ApiResponse({ status: 201 })
  async create(@CurrentUser() user: any, @Body() dto: CreateActivityDto) {
    return this.activities.create(dto, user.sub);
  }

  @Patch(':id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN, UserRole.MANAGER)
  @ApiOperation({ summary: 'Update an activity' })
  async update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: Partial<CreateActivityDto>) {
    return this.activities.update(id, dto);
  }

  @Post(':id/complete')
  @HttpCode(HttpStatus.OK)
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN, UserRole.MANAGER)
  @ApiOperation({ summary: 'Mark activity as completed' })
  async complete(
    @Param('id', ParseUUIDPipe) id: string,
    @CurrentUser() user: any,
    @Body() dto: CompleteActivityDto,
  ) {
    return this.activities.complete(id, dto, user.sub);
  }

  @Delete(':id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN, UserRole.MANAGER)
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Delete an activity' })
  async remove(@Param('id', ParseUUIDPipe) id: string) {
    await this.activities.remove(id);
  }
}
