// src/inquiry-notifications/inquiry-notifications.controller.ts
import {
  Controller, Get, Patch, Query, Param, Sse, MessageEvent,
} from '@nestjs/common';
import { Observable, map }                  from 'rxjs';
import { InquiryNotificationsService }      from './inquiry-notifications.service';
import { Public }                           from './../common/guards/decorators/public.decorator'; // ✅ ADDED

@Controller('inquiry-notifications')
export class InquiryNotificationsController {
  constructor(
    private readonly inquiryNotificationsService: InquiryNotificationsService,
  ) {}

  // ── SSE stream — each user connects with their userId ────────────────────
  // ✅ @Public() added — EventSource cannot send JWT headers
  @Public()
  @Sse('stream')
  stream(@Query('userId') userId: string): Observable<MessageEvent> {
    return this.inquiryNotificationsService.getEventsForUser$(+userId).pipe(
      map(event => ({ data: JSON.stringify(event) })),
    );
  }

  // ── Get all notifications for user (paginated) ────────────────────────────
  @Get()
  getAll(
    @Query('userId')      userId:       string,
    @Query('page')        page?:        string,
    @Query('limit')       limit?:       string,
    @Query('is_resolved') is_resolved?: string,
    @Query('type')        type?:        string,
  ) {
    return this.inquiryNotificationsService.getForUser(+userId, {
      page:        page  ? +page  : 1,
      limit:       limit ? +limit : 20,
      is_resolved: is_resolved !== undefined ? is_resolved === 'true' : undefined,
      type,
    });
  }

  // ── Get unread count — for bell badge ─────────────────────────────────────
  @Get('unread-count')
  getUnreadCount(@Query('userId') userId: string) {
    return this.inquiryNotificationsService.getUnreadCount(+userId)
      .then(count => ({ count }));
  }

  // ── Mark single as read ───────────────────────────────────────────────────
  @Patch(':id/read')
  markRead(
    @Param('id')     id:     string,
    @Query('userId') userId: string,
  ) {
    return this.inquiryNotificationsService.markAsRead(+id, +userId)
      .then(() => ({ success: true }));
  }
  // ── ✅ NEW — Mark single as unread ────────────────────────────────────────
  @Patch(':id/unread')
  markUnread(
    @Param('id')     id:     string,
    @Query('userId') userId: string,
  ) {
    return this.inquiryNotificationsService.markAsUnread(+id, +userId)
      .then(() => ({ success: true }));
  }
 
  // ── Mark all as read ──────────────────────────────────────────────────────
  @Patch('read-all')
  markAllRead(@Query('userId') userId: string) {
    return this.inquiryNotificationsService.markAllAsRead(+userId)
      .then(() => ({ success: true }));
  }

  // ── Mark single as resolved ───────────────────────────────────────────────
  @Patch(':id/resolve')
  markResolved(
    @Param('id')     id:     string,
    @Query('userId') userId: string,
  ) {
    return this.inquiryNotificationsService.markAsResolved(+id, +userId)
      .then(() => ({ success: true }));
  }

  // ── Mark all as resolved ──────────────────────────────────────────────────
  @Patch('resolve-all')
  markAllResolved(@Query('userId') userId: string) {
    return this.inquiryNotificationsService.markAllResolved(+userId)
      .then(() => ({ success: true }));
  }
}