import { Controller, Get, Post, Patch, Delete, Param, Body, Query, HttpCode, HttpStatus } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBody } from '@nestjs/swagger';
import { NotificationsService } from './notifications.service';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { PaginationDto } from '../../common/dto/pagination.dto';
import type { UserPayload } from '../../common/types';

@ApiTags('notifications')
@Controller('notifications')
export class NotificationsController {
  constructor(private readonly service: NotificationsService) {}

  @Get()
  @ApiOperation({ summary: '获取通知列表（含未读数）' })
  async list(@CurrentUser() user: UserPayload, @Query() pagination: PaginationDto) {
    return this.service.list(String(user?.id || 'anonymous'), pagination);
  }

  @Post(':id/read')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: '标记通知已读' })
  async markRead(@CurrentUser() user: UserPayload, @Param('id') id: string) {
    return this.service.markRead(String(user?.id || 'anonymous'), id);
  }

  @Post('read-all')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: '全部标记已读' })
  async markAllRead(@CurrentUser() user: UserPayload) {
    return this.service.markAllRead(String(user?.id || 'anonymous'));
  }

  // ═══ Preferences ═══

  @Get('preferences')
  @ApiOperation({ summary: '获取通知偏好' })
  async getPreferences(@CurrentUser() user: UserPayload) {
    return this.service.getPreferences(String(user?.id || 'anonymous'));
  }

  @Patch('preferences')
  @ApiOperation({ summary: '更新通知偏好' })
  @ApiBody({ schema: { type: 'object', properties: {
    reviewReminder: { type: 'boolean' },
    learningReminder: { type: 'boolean' },
    streakAlert: { type: 'boolean' },
    pushEnabled: { type: 'boolean' },
    quietStartHour: { type: 'number' },
    quietEndHour: { type: 'number' },
  } } })
  async updatePreferences(@CurrentUser() user: UserPayload, @Body() dto: Record<string, any>) {
    return this.service.updatePreferences(String(user?.id || 'anonymous'), dto);
  }

  // ═══ Push Tokens ═══

  @Get('push-tokens')
  @ApiOperation({ summary: '获取已注册的 Push Token' })
  async getPushTokens(@CurrentUser() user: UserPayload) {
    return this.service.getPushTokens(String(user?.id || 'anonymous'));
  }

  @Post('push-tokens')
  @ApiOperation({ summary: '注册 Push Token' })
  @ApiBody({ schema: { type: 'object', required: ['token', 'platform'], properties: {
    token: { type: 'string' },
    platform: { type: 'string', enum: ['ios', 'web'] },
    deviceId: { type: 'string' },
  } } })
  async registerPushToken(
    @CurrentUser() user: UserPayload,
    @Body() body: { token: string; platform: string; deviceId?: string },
  ) {
    return this.service.registerPushToken(String(user?.id || 'anonymous'), body.token, body.platform, body.deviceId);
  }

  @Delete('push-tokens/:token')
  @ApiOperation({ summary: '移除 Push Token' })
  async removePushToken(@CurrentUser() user: UserPayload, @Param('token') token: string) {
    return this.service.removePushToken(String(user?.id || 'anonymous'), token);
  }
}