import { Device } from '../../../domain/entities/device';
import { NotFoundError } from '../../../domain/errors/not-found-error';
import { IDeviceRepository } from '../../../domain/repositories/device-repository';
import { UpdateDevicePushTokenInput } from '../../dto/device.dto';

export class UpdateDevicePushTokenUseCase {
  constructor(private readonly deviceRepository: IDeviceRepository) {}

  async execute(id: string, input: UpdateDevicePushTokenInput): Promise<Device> {
    const device = await this.deviceRepository.findById(id);
    if (!device) {
      throw new NotFoundError('Device', id);
    }

    const updated = device.updatePushToken(input.pushToken);
    return this.deviceRepository.update(updated);
  }
}
