import * as plugins from '../../plugins.js';
import { DcRouterDb } from '../classes.dcrouter-db.js';

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

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

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

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

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

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

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

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

  constructor() {
    super();
  }

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

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