import * as plugins from '../../plugins.js';
import { DcRouterDb } from '../classes.dcrouter-db.js';
import type { IIpIntelligenceRecord } from '../../../ts_interfaces/data/security-policy.js';

const getDb = () => DcRouterDb.getInstance().getDb();

@plugins.smartdata.Collection(() => getDb())
export class IpIntelligenceDoc extends plugins.smartdata.SmartDataDbDoc<IpIntelligenceDoc, IpIntelligenceDoc> implements IIpIntelligenceRecord {
  @plugins.smartdata.unI()
  @plugins.smartdata.svDb()
  public ipAddress!: string;

  @plugins.smartdata.svDb()
  public asn: number | null = null;

  @plugins.smartdata.svDb()
  public asnOrg: string | null = null;

  @plugins.smartdata.svDb()
  public registrantOrg: string | null = null;

  @plugins.smartdata.svDb()
  public registrantCountry: string | null = null;

  @plugins.smartdata.svDb()
  public networkRange: string | null = null;

  @plugins.smartdata.svDb()
  public networkCidrs: string[] | null = null;

  @plugins.smartdata.svDb()
  public abuseContact: string | null = null;

  @plugins.smartdata.svDb()
  public country: string | null = null;

  @plugins.smartdata.svDb()
  public countryCode: string | null = null;

  @plugins.smartdata.svDb()
  public city: string | null = null;

  @plugins.smartdata.svDb()
  public latitude: number | null = null;

  @plugins.smartdata.svDb()
  public longitude: number | null = null;

  @plugins.smartdata.svDb()
  public accuracyRadius: number | null = null;

  @plugins.smartdata.svDb()
  public timezone: string | null = null;

  @plugins.smartdata.svDb()
  public firstSeenAt: number = Date.now();

  @plugins.smartdata.svDb()
  public lastSeenAt: number = Date.now();

  @plugins.smartdata.svDb()
  public updatedAt: number = Date.now();

  @plugins.smartdata.svDb()
  public seenCount: number = 0;

  constructor() {
    super();
  }

  public static async findByIp(ipAddress: string): Promise<IpIntelligenceDoc | null> {
    return await IpIntelligenceDoc.getInstance({ ipAddress });
  }

  public static async findAll(): Promise<IpIntelligenceDoc[]> {
    return await IpIntelligenceDoc.getInstances({});
  }
}
