import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  UseGuards,
  Req,
  Query,
} from "@nestjs/common"
import { NotificationService } from "./notification.service"
import { CreateNotificationDto } from "../dto/create-notification.dto"
import {
  ApiBearerAuth,
  ApiOkResponse,
  ApiQuery,
  ApiTags,
} from "@nestjs/swagger"
import { AuthGuardMiddleware } from "src/middleware/auth-guard.middleware"
import { QueryParams } from "src/common/interfaces/query-params.interface"
import { CustomerNotification } from "../entities/customer-notification.entity"
import { SendNotificationDto } from "../dto/send-notification.dto"
import { SendPushByFcmTokenDto } from "../dto/send-push-by-fcm-token.dto"
import { CustomerAuthGuardMiddleware } from "src/middleware/customer-auth-guard.middleware"
import { NotificationFilterDto } from "../dto/notification-filter.dto"

@ApiTags("Notification")
@ApiBearerAuth("access-token")
@Controller("notification")
export class NotificationController {
  constructor(private readonly notificationService: NotificationService) {}

  @UseGuards(AuthGuardMiddleware)
  @Post("create")
  create(@Body() createNotificationDto: CreateNotificationDto) {
    return this.notificationService.create(createNotificationDto)
  }

  @UseGuards(AuthGuardMiddleware)
  @Post("send")
  async sendNotification(@Body() sendNotificationDto: SendNotificationDto) {
    return this.notificationService.sendNotification(sendNotificationDto)
  }

  @UseGuards(AuthGuardMiddleware)
  @Post("send-by-fcm-token")
  async sendPushByFcmToken(@Body() body: SendPushByFcmTokenDto) {
    return this.notificationService.sendPushByFcmToken(body)
  }

  @UseGuards(AuthGuardMiddleware)
  @Get()
  findAll(@Req() req: any, @Query() filter: NotificationFilterDto) {
    const user_id = req.user?.user?.id || req.user?.id
    console.log("user from token", req.user)
    console.log("user_id", user_id)

    return this.notificationService.findAll(filter, user_id)
  }

  @UseGuards(AuthGuardMiddleware)
  @Get(":id")
  findOne(@Param("id") id: string) {
    return this.notificationService.findOne(+id)
  }

  @UseGuards(AuthGuardMiddleware)
  @Post(":id/mark-as-read")
  markAsRead(@Param("id") id: string) {
    return this.notificationService.markAsRead(+id)
  }

  @UseGuards(AuthGuardMiddleware)
  @Delete(":id")
  remove(@Param("id") id: string) {
    return this.notificationService.remove(+id)
  }

  // Customer Notification endpoints
  @UseGuards(CustomerAuthGuardMiddleware)
  @Get("customer/list")
  @ApiQuery({ name: "skip", required: false, type: Number })
  @ApiQuery({ name: "limit", required: false, type: Number })
  @ApiOkResponse({
    description: "Customer notifications list retrieved successfully",
    schema: {
      example: {
        success: true,
        code: 200,
        message: "Customer Notifications list retrieved successfully",
        data: {
          count: 10,
          data: [
            {
              id: 1,
              customer_id: 123,
              title: "New trip assigned",
              message: "You have been assigned a new trip",
              data: {
                trip_id: 456,
                type: "trip",
              },
              is_read: false,
              created_at: "2025-09-15T08:44:48.819Z",
            },
          ],
          pagination: {
            total: 10,
            page: 1,
            limit: 1,
            totalPages: 10,
          },
          unreadCount: 5,
        },
      },
    },
  })
  findAllCustomerNotifications(
    @Req() req: any,
    @Query("skip") skip: number = 0,
    @Query("limit") limit: number = 10,
  ) {
    const take = limit
    const params: QueryParams<CustomerNotification> = {
      take,
      skip,
      where: {
        customer_id: req.customer.customer_id,
      },
    }
    return this.notificationService.findAllCustomerNotifications(params)
  }

  @UseGuards(CustomerAuthGuardMiddleware)
  @Get("customer/:id")
  findOneCustomerNotification(@Param("id") id: string) {
    return this.notificationService.findOneCustomerNotification(+id)
  }

  @UseGuards(CustomerAuthGuardMiddleware)
  @Post("customer/:id/mark-as-read")
  markCustomerNotificationAsRead(@Param("id") id: string) {
    return this.notificationService.markCustomerNotificationAsRead(+id)
  }

  @UseGuards(CustomerAuthGuardMiddleware)
  @Delete("customer/:id")
  removeCustomerNotification(@Param("id") id: string) {
    return this.notificationService.removeCustomerNotification(+id)
  }
}
