import { BaseModel } from "./BaseModel";

export class GlobalKey extends BaseModel {
  static tableName = "global_keys";

  id!: number;
  global_keys!: Record<string, any>; // JSONB field
  created_at?: string;
  updated_at?: string;
  deleted_at?: string | null;

  $formatDatabaseJson(json: any) {
    json = super.$formatDatabaseJson(json);

    const formatDate = (d: Date | string) => {
      if (!d) return null;
      const date = new Date(d);
      const pad = (n: number) => n.toString().padStart(2, "0");
      return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(
        date.getDate()
      )} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(
        date.getSeconds()
      )}`;
    };

    if (json.created_at) json.created_at = formatDate(json.created_at);
    if (json.updated_at) json.updated_at = formatDate(json.updated_at);
    if (json.deleted_at) json.deleted_at = formatDate(json.deleted_at);

    return json;
  }
}
