import { Injectable } from '@nestjs/common';
import { WaitlistRepository } from './waitlist.repository';
import { CreateWaitlistDto } from './dto/create-waitlist.dto';

@Injectable()
export class WaitlistService {
  constructor(private readonly repository: WaitlistRepository) {}

  async create(dto: CreateWaitlistDto) {
    const existing = await this.repository.findAll();
    const duplicate = existing.find((e) => e.email === dto.email);
    if (duplicate) throw new Error('该邮箱已报名');
    return this.repository.create({
      nickname: dto.nickname || '',
      email: dto.email,
      devices: dto.devices || [],
      interests: dto.interests || [],
      painpoint: dto.painpoint || '',
      willingBeta: dto.willingBeta || false,
    });
  }

  async findAll() {
    return this.repository.findAll();
  }

  async getStats() {
    const entries = await this.repository.findAll();
    return {
      total: entries.length,
      betaUsers: entries.filter((e) => e.willingBeta).length,
    };
  }
}
