import * as plugins from '../../plugins.js';
import { DcRouterDb } from '../classes.dcrouter-db.js';
import type {
  IEmailDomainDkim,
  IEmailDomainRateLimits,
  IEmailDomainDnsStatus,
} from '../../../ts_interfaces/data/email-domain.js';

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

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

  @plugins.smartdata.svDb()
  public domain: string = '';

  @plugins.smartdata.svDb()
  public linkedDomainId: string = '';

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

  @plugins.smartdata.svDb()
  public dkim!: IEmailDomainDkim;

  @plugins.smartdata.svDb()
  public rateLimits?: IEmailDomainRateLimits;

  @plugins.smartdata.svDb()
  public dnsStatus!: IEmailDomainDnsStatus;

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

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

  constructor() {
    super();
  }

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

  public static async findByDomain(domain: string): Promise<EmailDomainDoc | null> {
    return await EmailDomainDoc.getInstance({ domain: domain.toLowerCase() });
  }

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