import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';

@Injectable()
export class DeviceStatusService {
  constructor(private readonly prisma: PrismaService) {}

  // A device that has never registered (never sent data, never added in the
  // UI) isn't disabled by anyone — treat it as active so first contact works.
  async getStatus(serial: string) {
    const device = await this.prisma.device.findUnique({
      where: { serialNumber: serial },
      select: { isActive: true, disabledReason: true },
    });
    return {
      serial,
      registered: device !== null,
      disabled: device ? !device.isActive : false,
      reason: device?.disabledReason ?? null,
    };
  }
}
