import {
  Controller,
  Get,
  Put,
  Post,
  Body,
  Req,
  UseGuards,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  UserPreferencesService,
  UserPreferences,
  NotificationPreferences,
} from './user-preferences.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@Controller('api/v1/preferences')
@UseGuards(JwtAuthGuard)
export class UserPreferencesController {
  constructor(
    private readonly userPreferencesService: UserPreferencesService,
  ) {}

  /**
   * Get user preferences
   */
  @Get()
  @HttpCode(HttpStatus.OK)
  async getPreferences(@Req() req: any): Promise<UserPreferences> {
    const userId = req.user?.userId;
    return this.userPreferencesService.getUserPreferences(userId);
  }

  /**
   * Update user preferences
   */
  @Put()
  @HttpCode(HttpStatus.OK)
  async updatePreferences(
    @Req() req: any,
    @Body() preferences: Partial<UserPreferences>,
  ): Promise<UserPreferences> {
    const userId = req.user?.userId;
    return this.userPreferencesService.updateUserPreferences(
      userId,
      preferences,
    );
  }

  /**
   * Update notification preferences
   */
  @Put('notifications')
  @HttpCode(HttpStatus.OK)
  async updateNotificationPreferences(
    @Req() req: any,
    @Body() notificationPreferences: Partial<NotificationPreferences>,
  ): Promise<UserPreferences> {
    const userId = req.user?.userId;
    return this.userPreferencesService.updateNotificationPreferences(
      userId,
      notificationPreferences,
    );
  }

  /**
   * Enable push notifications
   */
  @Post('notifications/push/enable')
  @HttpCode(HttpStatus.OK)
  async enablePushNotifications(@Req() req: any): Promise<{ success: boolean }> {
    const userId = req.user?.userId;
    await this.userPreferencesService.enablePushNotifications(userId);
    return { success: true };
  }

  /**
   * Disable push notifications
   */
  @Post('notifications/push/disable')
  @HttpCode(HttpStatus.OK)
  async disablePushNotifications(
    @Req() req: any,
  ): Promise<{ success: boolean }> {
    const userId = req.user?.userId;
    await this.userPreferencesService.disablePushNotifications(userId);
    return { success: true };
  }

  /**
   * Get notification preferences summary
   */
  @Get('notifications/summary')
  @HttpCode(HttpStatus.OK)
  async getNotificationSummary(@Req() req: any): Promise<{
    enabled: boolean;
    enabledTypes: string[];
    disabledTypes: string[];
  }> {
    const userId = req.user?.userId;
    return this.userPreferencesService.getNotificationPreferencesSummary(
      userId,
    );
  }

  /**
   * Reset preferences to defaults
   */
  @Post('reset')
  @HttpCode(HttpStatus.OK)
  async resetPreferences(@Req() req: any): Promise<UserPreferences> {
    const userId = req.user?.userId;
    return this.userPreferencesService.resetToDefaults(userId);
  }
}
