import { Controller, Get, Put, Param, UseGuards, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { Notification } from './entities/notification.entity';
import { NotificationService } from './notification.service';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { User } from '../user/entities/user.entity';
import { SearchNotificationDto } from './dto/search-notification.dto'; // ✅ Import
import { ParseUuidPipe } from '../../common/pipes/parse-uuid.pipe';

@ApiTags('Notifications')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('notifications')
export class NotificationController {
  constructor(
    private readonly notificationService: NotificationService,
    @InjectRepository(Notification)
    private notificationRepo: Repository<Notification>
  ) {}

  @Get()
  @ApiOperation({ summary: 'Get my notifications' })
  async getMyNotifications(
    @CurrentUser() user: User,
    @Query() searchDto: SearchNotificationDto // ✅ ใช้ DTO แทน
  ) {
    const { page = 1, limit = 20, isRead } = searchDto;

    const where: Record<string, unknown> = { userId: user.user_id };

    // เพิ่ม Filter isRead ถ้ามีการส่งมา
    if (isRead !== undefined) {
      where.isRead = isRead;
    }

    const [items, total] = await this.notificationRepo.findAndCount({
      where,
      order: { createdAt: 'DESC' },
      take: limit,
      skip: (page - 1) * limit,
    });

    const unreadCount = await this.notificationRepo.count({
      where: { userId: user.user_id, isRead: false },
    });

    return { data: items, meta: { total, page, limit, unreadCount } };
  }

  @Get('unread')
  @ApiOperation({ summary: 'Get unread notification count' })
  async getUnreadCount(@CurrentUser() user: User) {
    const count = await this.notificationService.getUnreadCount(user.user_id);
    return { unreadCount: count };
  }

  @Put(':uuid/read')
  @ApiOperation({ summary: 'Mark notification as read' })
  async markAsRead(
    @Param('uuid', ParseUuidPipe) uuid: string,
    @CurrentUser() user: User
  ) {
    return this.notificationService.markAsReadByUuid(uuid, user.user_id);
  }

  @Put('read-all')
  @ApiOperation({ summary: 'Mark all as read' })
  async markAllAsRead(@CurrentUser() user: User) {
    return this.notificationService.markAllAsRead(user.user_id);
  }
}
