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

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

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

  /** FQDN — kept lowercased on save. */
  @plugins.smartdata.svDb()
  public name: string = '';

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

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

  @plugins.smartdata.svDb()
  public authoritative: boolean = false;

  @plugins.smartdata.svDb()
  public nameservers?: string[];

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

  @plugins.smartdata.svDb()
  public lastSyncedAt?: number;

  @plugins.smartdata.svDb()
  public description?: 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<DomainDoc | null> {
    return await DomainDoc.getInstance({ id });
  }

  public static async findByName(name: string): Promise<DomainDoc | null> {
    return await DomainDoc.getInstance({ name: name.toLowerCase() });
  }

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

  public static async findByProviderId(providerId: string): Promise<DomainDoc[]> {
    return await DomainDoc.getInstances({ providerId });
  }
}
