import * as plugins from '../../plugins.js';
import { DcRouterDb } from '../classes.dcrouter-db.js';
import type { TDnsRecordType, TDnsRecordSource } from '../../../ts_interfaces/data/dns-record.js';

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

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

  @plugins.smartdata.svDb()
  public domainId!: string;

  /** FQDN of the record (e.g. 'www.example.com'). */
  @plugins.smartdata.svDb()
  public name: string = '';

  @plugins.smartdata.svDb()
  public type!: TDnsRecordType;

  @plugins.smartdata.svDb()
  public value!: string;

  @plugins.smartdata.svDb()
  public ttl: number = 300;

  @plugins.smartdata.svDb()
  public proxied?: boolean;

  @plugins.smartdata.svDb()
  public source!: TDnsRecordSource;

  @plugins.smartdata.svDb()
  public providerRecordId?: string;

  @plugins.smartdata.svDb()
  public createdAt!: number;

  @plugins.smartdata.svDb()
  public updatedAt!: number;

  @plugins.smartdata.svDb()
  public createdBy!: string;

  constructor() {
    super();
  }

  public static async findById(id: string): Promise<DnsRecordDoc | null> {
    return await DnsRecordDoc.getInstance({ id });
  }

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

  public static async findByDomainId(domainId: string): Promise<DnsRecordDoc[]> {
    return await DnsRecordDoc.getInstances({ domainId });
  }
}
