import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Req,
  Param,
  Query,
  Body,
} from '@nestjs/common';
import { NotificationsService } from './notifications.service';
import {
  NotificationType,
  NotificationPriority,
  NotificationStatus,
} from '../../database/entities/notification.entity';

@Controller('api/v1/notifications')
export class NotificationsController {
  constructor(private readonly notificationsService: NotificationsService) {}

  @Get('smart')
  async getSmartNotifications(@Req() req: any) {
    const suggestions = await this.notificationsService.getSmartNotifications(
      req.user.userId,
    );
    return {
      success: true,
      data: { suggestions },
    };
  }

  @Get('medications')
  async getMedicationReminders(@Req() req: any) {
    const reminders = await this.notificationsService.getMedicationReminders(
      req.user.userId,
    );
    return {
      success: true,
      data: { reminders },
    };
  }

  @Get('milestones/:childId')
  async getMilestones(@Req() req: any, @Param('childId') childId: string) {
    const milestones =
      await this.notificationsService.detectMilestones(childId);
    return {
      success: true,
      data: { milestones },
    };
  }

  @Get('anomalies/:childId')
  async getAnomalies(@Req() req: any, @Param('childId') childId: string) {
    const anomalies = await this.notificationsService.detectAnomalies(childId);
    return {
      success: true,
      data: { anomalies },
    };
  }

  @Get()
  async getNotifications(
    @Req() req: any,
    @Query('status') status?: NotificationStatus,
    @Query('limit') limit?: string,
    @Query('includeRead') includeRead?: string,
  ) {
    const notifications = await this.notificationsService.getUserNotifications(
      req.user.userId,
      {
        status,
        limit: limit ? parseInt(limit, 10) : undefined,
        includeRead: includeRead === 'true',
      },
    );
    return {
      success: true,
      data: { notifications },
    };
  }

  @Post()
  async createNotification(
    @Req() req: any,
    @Body()
    body: {
      type: NotificationType;
      title: string;
      message: string;
      childId?: string;
      priority?: NotificationPriority;
      metadata?: any;
      scheduledFor?: string;
    },
  ) {
    const notification = await this.notificationsService.createNotification(
      req.user.userId,
      body.type,
      body.title,
      body.message,
      {
        childId: body.childId,
        priority: body.priority,
        metadata: body.metadata,
        scheduledFor: body.scheduledFor
          ? new Date(body.scheduledFor)
          : undefined,
      },
    );
    return {
      success: true,
      data: { notification },
    };
  }

  @Post('growth-reminder/:childId')
  async scheduleGrowthReminder(
    @Req() req: any,
    @Param('childId') childId: string,
  ) {
    const notification = await this.notificationsService.scheduleGrowthReminder(
      req.user.userId,
      childId,
    );
    return {
      success: true,
      data: { notification },
    };
  }

  @Patch(':notificationId/read')
  async markAsRead(
    @Req() req: any,
    @Param('notificationId') notificationId: string,
  ) {
    await this.notificationsService.markAsRead(notificationId, req.user.userId);
    return {
      success: true,
      message: 'Notification marked as read',
    };
  }

  @Patch(':notificationId/dismiss')
  async dismissNotification(
    @Req() req: any,
    @Param('notificationId') notificationId: string,
  ) {
    await this.notificationsService.dismiss(notificationId, req.user.userId);
    return {
      success: true,
      message: 'Notification dismissed',
    };
  }

  @Patch('mark-all-read')
  async markAllAsRead(@Req() req: any) {
    await this.notificationsService.markAllAsRead(req.user.userId);
    return {
      success: true,
      message: 'All notifications marked as read',
    };
  }

  @Delete('cleanup')
  async cleanupOldNotifications(@Query('daysOld') daysOld?: string) {
    const deletedCount =
      await this.notificationsService.cleanupOldNotifications(
        daysOld ? parseInt(daysOld, 10) : 30,
      );
    return {
      success: true,
      data: { deletedCount },
      message: `Cleaned up ${deletedCount} old notifications`,
    };
  }
}
