import * as plugins from '../../plugins.js';
import { DcRouterDb } from '../classes.dcrouter-db.js';
import type { IRouteMetadata } from '../../../ts_interfaces/data/route-management.js';
import type { IDcRouterRouteConfig } from '../../../ts_interfaces/data/remoteingress.js';

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

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

  @plugins.smartdata.svDb()
  public route!: IDcRouterRouteConfig;

  @plugins.smartdata.svDb()
  public enabled!: boolean;

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

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

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

  @plugins.smartdata.svDb()
  public origin!: 'config' | 'email' | 'dns' | 'api';

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

  @plugins.smartdata.svDb()
  public metadata?: IRouteMetadata;

  constructor() {
    super();
  }

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

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

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

  public static async findByOrigin(origin: 'config' | 'email' | 'dns' | 'api'): Promise<RouteDoc[]> {
    return await RouteDoc.getInstances({ origin });
  }

  public static async findBySystemKey(systemKey: string): Promise<RouteDoc | null> {
    return await RouteDoc.getInstance({ systemKey });
  }
}
