import * as plugins from '../../plugins.js';
import { DcRouterDb } from '../classes.dcrouter-db.js';
import type {
  TDnsProviderType,
  TDnsProviderStatus,
  TDnsProviderCredentials,
} from '../../../ts_interfaces/data/dns-provider.js';

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

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

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

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

  /**
   * Provider credentials, persisted as an opaque object. Shape varies by `type`.
   * Never returned to the UI — handlers map to IDnsProviderPublic before sending.
   */
  @plugins.smartdata.svDb()
  public credentials!: TDnsProviderCredentials;

  @plugins.smartdata.svDb()
  public status: TDnsProviderStatus = 'untested';

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

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

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

  public static async findByType(type: TDnsProviderType): Promise<DnsProviderDoc[]> {
    return await DnsProviderDoc.getInstances({ type });
  }
}
