import AcmeCertificate from "./AcmeCertificate";
import AcmeChallenge from "./AcmeChallenge";
import KubernetesCluster from "./KubernetesCluster";
// API Keys
import ApiKey from "./ApiKey";
import ApiKeyPermission from "./ApiKeyPermission";
import BillingInvoice from "./BillingInvoice";
import BillingPaymentMethods from "./BillingPaymentMethod";
import CallLog from "./CallLog";
// Date migration
import DataMigration from "./DataMigration";
import Domain from "./Domain";
import EmailLog from "./EmailLog";
import EmailVerificationToken from "./EmailVerificationToken";
import File from "./File";
import GlobalConfig from "./GlobalConfig";
import GreenlockCertificate from "./GreenlockCertificate";
// Greenlock
import GreenlockChallenge from "./GreenlockChallenge";
// Incidents
import Incident from "./Incident";
import IncidentFeed from "./IncidentFeed";
import IncidentCustomField from "./IncidentCustomField";
import IncidentInternalNote from "./IncidentInternalNote";
import IncidentNoteTemplate from "./IncidentNoteTemplate";
import IncidentPostmortemTemplate from "./IncidentPostmortemTemplate";
import IncidentOwnerTeam from "./IncidentOwnerTeam";
import IncidentOwnerUser from "./IncidentOwnerUser";
import IncidentRole from "./IncidentRole";
import IncidentMember from "./IncidentMember";
import IncidentPublicNote from "./IncidentPublicNote";
import IncidentSeverity from "./IncidentSeverity";
import IncidentState from "./IncidentState";
import IncidentStateTimeline from "./IncidentStateTimeline";
import IncidentTemplate from "./IncidentTemplate";
import IncidentTemplateOwnerTeam from "./IncidentTemplateOwnerTeam";
import IncidentTemplateOwnerUser from "./IncidentTemplateOwnerUser";
//Labels.
import Label from "./Label";
import LogSavedView from "./LogSavedView";
import LogPipeline from "./LogPipeline";
import LogPipelineProcessor from "./LogPipelineProcessor";
import LogDropFilter from "./LogDropFilter";
import LogScrubRule from "./LogScrubRule";
// Monitors
import Monitor from "./Monitor";
import MonitorCustomField from "./MonitorCustomField";
// Monitor Groups
import MonitorGroup from "./MonitorGroup";
import MonitorGroupOwnerTeam from "./MonitorGroupOwnerTeam";
import MonitorGroupOwnerUser from "./MonitorGroupOwnerUser";
import MonitorGroupResource from "./MonitorGroupResource";
import MonitorOwnerTeam from "./MonitorOwnerTeam";
import MonitorOwnerUser from "./MonitorOwnerUser";
import MonitorProbe from "./MonitorProbe";
import MonitorSecret from "./MonitorSecret";
import MonitorStatus from "./MonitorStatus";
import MonitorStatusTimeline from "./MonitorStatusTimeline";
// On-Call Duty
import OnCallDutyPolicy from "./OnCallDutyPolicy";
import OnCallDutyPolicyCustomField from "./OnCallDutyPolicyCustomField";
import OnCallDutyPolicyEscalationRule from "./OnCallDutyPolicyEscalationRule";
import OnCallDutyPolicyEscalationRuleSchedule from "./OnCallDutyPolicyEscalationRuleSchedule";
import OnCallDutyPolicyEscalationRuleTeam from "./OnCallDutyPolicyEscalationRuleTeam";
import OnCallDutyPolicyEscalationRuleUser from "./OnCallDutyPolicyEscalationRuleUser";
import OnCallDutyPolicyExecutionLog from "./OnCallDutyPolicyExecutionLog";
import OnCallDutyPolicyExecutionLogTimeline from "./OnCallDutyPolicyExecutionLogTimeline";
// owner team
import OnCallDutyPolicyOwnerTeam from "./OnCallDutyPolicyOwnerTeam";
import OnCallDutyPolicyOwnerUser from "./OnCallDutyPolicyOwnerUser";

// OnCall Duty Policy Feed
import OnCallDutyPolicyFeed from "./OnCallDutyPolicyFeed";
// On call duty policy schedule
import OnCallDutyPolicySchedule from "./OnCallDutyPolicySchedule";
import OnCallDutyPolicyScheduleLayer from "./OnCallDutyPolicyScheduleLayer";
import OnCallDutyPolicyScheduleLayerUser from "./OnCallDutyPolicyScheduleLayerUser";
import OnCallDutyPolicyTimeLog from "./OnCallDutyPolicyTimeLog";

// Incoming Call Policy
import IncomingCallPolicy from "./IncomingCallPolicy";
import IncomingCallPolicyEscalationRule from "./IncomingCallPolicyEscalationRule";
import IncomingCallLog from "./IncomingCallLog";
import IncomingCallLogItem from "./IncomingCallLogItem";

import Probe from "./Probe";
import ProbeOwnerTeam from "./ProbeOwnerTeam";
import ProbeOwnerUser from "./ProbeOwnerUser";
import AIAgent from "./AIAgent";
import AIAgentOwnerTeam from "./AIAgentOwnerTeam";
import AIAgentOwnerUser from "./AIAgentOwnerUser";
import AIAgentTask from "./AIAgentTask";
import AIAgentTaskLog from "./AIAgentTaskLog";
import AIAgentTaskPullRequest from "./AIAgentTaskPullRequest";
import AIAgentTaskTelemetryException from "./AIAgentTaskTelemetryException";
import LlmProvider from "./LlmProvider";
import LlmLog from "./LlmLog";
import Project from "./Project";
import ProjectCallSMSConfig from "./ProjectCallSMSConfig";
import ProjectUserProfile from "./ProjectUserProfile";
// Project SMTP Config.
import ProjectSmtpConfig from "./ProjectSmtpConfig";
//SSO
import ProjectSSO from "./ProjectSso";
import PromoCode from "./PromoCode";
import EnterpriseLicense from "./EnterpriseLicense";
import OpenSourceDeployment from "./OpenSourceDeployment";
import Reseller from "./Reseller";
import ResellerPlan from "./ResellerPlan";
// ScheduledMaintenances
import ScheduledMaintenance from "./ScheduledMaintenance";
import ScheduledMaintenanceCustomField from "./ScheduledMaintenanceCustomField";
import ScheduledMaintenanceInternalNote from "./ScheduledMaintenanceInternalNote";
import ScheduledMaintenanceNoteTemplate from "./ScheduledMaintenanceNoteTemplate";
import ScheduledMaintenanceOwnerTeam from "./ScheduledMaintenanceOwnerTeam";
import ScheduledMaintenanceOwnerUser from "./ScheduledMaintenanceOwnerUser";
import ScheduledMaintenancePublicNote from "./ScheduledMaintenancePublicNote";
import ScheduledMaintenanceState from "./ScheduledMaintenanceState";
import ScheduledMaintenanceStateTimeline from "./ScheduledMaintenanceStateTimeline";
import Service from "./Service";
import ServiceOwnerTeam from "./ServiceOwnerTeam";
import ServiceOwnerUser from "./ServiceOwnerUser";
import ServiceCodeRepository from "./ServiceCodeRepository";
import CodeRepository from "./CodeRepository";
// Short link.
import ShortLink from "./ShortLink";
// SMS
import SmsLog from "./SmsLog";
import WhatsAppLog from "./WhatsAppLog";
import PushNotificationLog from "./PushNotificationLog";
import WorkspaceNotificationLog from "./WorkspaceNotificationLog";
// Status Page
import StatusPage from "./StatusPage";
import StatusPageAnnouncement from "./StatusPageAnnouncement";
import StatusPageAnnouncementTemplate from "./StatusPageAnnouncementTemplate";
import StatusPageCustomField from "./StatusPageCustomField";
import StatusPageDomain from "./StatusPageDomain";
import StatusPageFooterLink from "./StatusPageFooterLink";
import StatusPageGroup from "./StatusPageGroup";
import StatusPageHeaderLink from "./StatusPageHeaderLink";
import StatusPageHistoryChartBarColorRule from "./StatusPageHistoryChartBarColorRule";
import StatusPageOwnerTeam from "./StatusPageOwnerTeam";
import StatusPageOwnerUser from "./StatusPageOwnerUser";
import StatusPagePrivateUser from "./StatusPagePrivateUser";
import StatusPagePrivateUserSession from "./StatusPagePrivateUserSession";
import StatusPageResource from "./StatusPageResource";
import StatusPageSCIM from "./StatusPageSCIM";
import StatusPageSSO from "./StatusPageSso";
import StatusPageSubscriber from "./StatusPageSubscriber";
import StatusPageSubscriberNotificationTemplate from "./StatusPageSubscriberNotificationTemplate";
import StatusPageSubscriberNotificationTemplateStatusPage from "./StatusPageSubscriberNotificationTemplateStatusPage";
// Team
import Team from "./Team";
import TeamMember from "./TeamMember";
import TeamMemberCustomField from "./TeamMemberCustomField";
import TeamPermission from "./TeamPermission";
import TeamComplianceSetting from "./TeamComplianceSetting";
import UsageBilling from "./TelemetryUsageBilling";
import User from "./User";
import UserSession from "./UserSession";
import UserCall from "./UserCall";
// Notification Methods
import UserEmail from "./UserEmail";
import UserPush from "./UserPush";
import UserWhatsApp from "./UserWhatsApp";
// User Notification Rules
import UserNotificationRule from "./UserNotificationRule";
import UserNotificationSetting from "./UserNotificationSetting";
import UserOnCallLog from "./UserOnCallLog";
import UserOnCallLogTimeline from "./UserOnCallLogTimeline";
import UserSms from "./UserSMS";
import UserIncomingCallNumber from "./UserIncomingCallNumber";
// Workflows.
import Workflow from "./Workflow";
import WorkflowLog from "./WorkflowLog";
import WorkflowVariables from "./WorkflowVariable";
import ServiceDependency from "./ServiceDependency";
import ServiceMonitor from "./ServiceMonitor";

import UserTotpAuth from "./UserTotpAuth";
import UserWebAuthn from "./UserWebAuthn";

import TelemetryIngestionKey from "./TelemetryIngestionKey";

import TelemetryException from "./TelemetryException";
import ScheduledMaintenanceTemplate from "./ScheduledMaintenanceTemplate";
import ScheduledMaintenanceTemplateOwnerTeam from "./ScheduledMaintenanceTemplateOwnerTeam";
import ScheduledMaintenanceTemplateOwnerUser from "./ScheduledMaintenanceTemplateOwnerUser";

import BaseModel from "./DatabaseBaseModel/DatabaseBaseModel";

import AlertState from "./AlertState";
import Alert from "./Alert";
import AlertCustomField from "./AlertCustomField";
import AlertStateTimeline from "./AlertStateTimeline";
import AlertInternalNote from "./AlertInternalNote";
import AlertOwnerTeam from "./AlertOwnerTeam";
import AlertOwnerUser from "./AlertOwnerUser";
import AlertSeverity from "./AlertSeverity";
import AlertNoteTemplate from "./AlertNoteTemplate";
import AlertFeed from "./AlertFeed";

import AlertEpisode from "./AlertEpisode";
import AlertEpisodeMember from "./AlertEpisodeMember";
import AlertEpisodeStateTimeline from "./AlertEpisodeStateTimeline";
import AlertEpisodeOwnerUser from "./AlertEpisodeOwnerUser";
import AlertEpisodeOwnerTeam from "./AlertEpisodeOwnerTeam";
import AlertEpisodeInternalNote from "./AlertEpisodeInternalNote";
import AlertEpisodeFeed from "./AlertEpisodeFeed";
import AlertGroupingRule from "./AlertGroupingRule";

import IncidentEpisode from "./IncidentEpisode";
import IncidentEpisodeMember from "./IncidentEpisodeMember";
import IncidentEpisodeRoleMember from "./IncidentEpisodeRoleMember";
import IncidentEpisodeStateTimeline from "./IncidentEpisodeStateTimeline";
import IncidentEpisodeOwnerUser from "./IncidentEpisodeOwnerUser";
import IncidentEpisodeOwnerTeam from "./IncidentEpisodeOwnerTeam";
import IncidentEpisodeInternalNote from "./IncidentEpisodeInternalNote";
import IncidentEpisodeFeed from "./IncidentEpisodeFeed";
import IncidentEpisodePublicNote from "./IncidentEpisodePublicNote";
import IncidentGroupingRule from "./IncidentGroupingRule";
import IncidentSlaRule from "./IncidentSlaRule";
import IncidentSla from "./IncidentSla";

import TableView from "./TableView";
import Dashboard from "./Dashboard";
import DashboardDomain from "./DashboardDomain";

import MonitorTest from "./MonitorTest";
import ScheduledMaintenanceFeed from "./ScheduledMaintenanceFeed";

import WorkspaceUserAuthToken from "./WorkspaceUserAuthToken";
import WorkspaceProjectAuthToken from "./WorkspaceProjectAuthToken";
import WorkspaceSetting from "./WorkspaceSetting";
import WorkspaceNotificationRule from "./WorkspaceNotificationRule";
import WorkspaceNotificationSummary from "./WorkspaceNotificationSummary";

import OnCallDutyPolicyUserOverride from "./OnCallDutyPolicyUserOverride";
import MonitorFeed from "./MonitorFeed";
import MetricType from "./MetricType";
import ProjectSCIM from "./ProjectSCIM";
import ProjectSCIMLog from "./ProjectSCIMLog";
import StatusPageSCIMLog from "./StatusPageSCIMLog";

const AllModelTypes: Array<{
  new (): BaseModel;
}> = [
  User,
  WorkspaceUserAuthToken,
  WorkspaceProjectAuthToken,
  Probe,
  Project,
  EmailVerificationToken,
  Team,
  TeamMember,
  TeamMemberCustomField,
  TeamPermission,
  TeamComplianceSetting,
  ApiKey,
  Label,
  LogSavedView,
  LogPipeline,
  LogPipelineProcessor,
  LogDropFilter,
  LogScrubRule,
  ApiKeyPermission,
  ProjectSmtpConfig,
  StatusPage,

  OnCallDutyPolicy,
  OnCallDutyPolicyCustomField,
  OnCallDutyPolicyEscalationRule,
  OnCallDutyPolicyEscalationRuleTeam,
  OnCallDutyPolicyEscalationRuleUser,
  OnCallDutyPolicyExecutionLog,
  OnCallDutyPolicyExecutionLogTimeline,
  OnCallDutyPolicyUserOverride,

  // Incoming Call Policy
  IncomingCallPolicy,
  IncomingCallPolicyEscalationRule,
  IncomingCallLog,
  IncomingCallLogItem,

  Monitor,
  MonitorSecret,
  MonitorStatus,
  MonitorCustomField,

  IncidentState,
  Incident,
  IncidentFeed,
  IncidentCustomField,
  IncidentStateTimeline,
  IncidentInternalNote,
  IncidentPublicNote,
  IncidentTemplate,
  IncidentTemplateOwnerTeam,
  IncidentTemplateOwnerUser,
  IncidentOwnerTeam,
  IncidentOwnerUser,
  IncidentRole,
  IncidentMember,
  IncidentSeverity,
  IncidentNoteTemplate,
  IncidentPostmortemTemplate,

  AlertState,
  Alert,
  AlertFeed,
  AlertCustomField,
  AlertStateTimeline,
  AlertInternalNote,
  AlertOwnerTeam,
  AlertOwnerUser,
  AlertSeverity,
  AlertNoteTemplate,

  AlertEpisode,
  AlertEpisodeMember,
  AlertEpisodeStateTimeline,
  AlertEpisodeOwnerUser,
  AlertEpisodeOwnerTeam,
  AlertEpisodeInternalNote,
  AlertEpisodeFeed,
  AlertGroupingRule,

  IncidentEpisode,
  IncidentEpisodeMember,
  IncidentEpisodeRoleMember,
  IncidentEpisodeStateTimeline,
  IncidentEpisodeOwnerUser,
  IncidentEpisodeOwnerTeam,
  IncidentEpisodeInternalNote,
  IncidentEpisodeFeed,
  IncidentEpisodePublicNote,
  IncidentGroupingRule,
  IncidentSlaRule,
  IncidentSla,

  MonitorStatusTimeline,

  File,
  Domain,

  StatusPageGroup,
  StatusPageDomain,
  StatusPageCustomField,
  StatusPageResource,

  StatusPageAnnouncement,
  StatusPageAnnouncementTemplate,
  StatusPageSubscriber,
  StatusPageSubscriberNotificationTemplate,
  StatusPageSubscriberNotificationTemplateStatusPage,
  StatusPageFooterLink,
  StatusPageHeaderLink,
  StatusPagePrivateUser,
  StatusPagePrivateUserSession,
  StatusPageHistoryChartBarColorRule,

  ScheduledMaintenanceState,
  ScheduledMaintenance,
  ScheduledMaintenanceStateTimeline,
  ScheduledMaintenancePublicNote,
  ScheduledMaintenanceInternalNote,
  ScheduledMaintenanceCustomField,
  ScheduledMaintenanceFeed,

  BillingPaymentMethods,
  BillingInvoice,

  GreenlockChallenge,
  GreenlockCertificate,

  Workflow,
  WorkflowVariables,
  WorkflowLog,

  ProjectSSO,
  StatusPageSSO,
  StatusPageSCIM,

  MonitorProbe,

  MonitorOwnerTeam,
  MonitorOwnerUser,

  ScheduledMaintenanceOwnerTeam,
  ScheduledMaintenanceOwnerUser,

  StatusPageOwnerTeam,
  StatusPageOwnerUser,

  SmsLog,
  WhatsAppLog,
  PushNotificationLog,
  WorkspaceNotificationLog,
  CallLog,
  EmailLog,

  UserEmail,
  UserSms,
  UserCall,
  UserPush,
  UserWhatsApp,
  UserIncomingCallNumber,

  UserNotificationRule,
  UserOnCallLog,
  UserOnCallLogTimeline,
  UserNotificationSetting,

  DataMigration,

  ShortLink,

  ScheduledMaintenanceTemplate,
  ScheduledMaintenanceTemplateOwnerTeam,
  ScheduledMaintenanceTemplateOwnerUser,
  ScheduledMaintenanceNoteTemplate,

  Reseller,
  ResellerPlan,

  PromoCode,
  EnterpriseLicense,
  OpenSourceDeployment,

  GlobalConfig,

  MonitorGroup,
  MonitorGroupOwnerTeam,
  MonitorGroupOwnerUser,
  MonitorGroupResource,

  OnCallDutyPolicySchedule,
  OnCallDutyPolicyScheduleLayer,
  OnCallDutyPolicyScheduleLayerUser,
  OnCallDutyPolicyFeed,
  OnCallDutyPolicyOwnerTeam,
  OnCallDutyPolicyOwnerUser,
  OnCallDutyPolicyEscalationRuleSchedule,

  UsageBilling,

  ProjectCallSMSConfig,
  ProjectUserProfile,

  AcmeCertificate,

  AcmeChallenge,

  Service,
  ServiceOwnerTeam,
  ServiceOwnerUser,
  ServiceDependency,
  ServiceMonitor,
  ServiceCodeRepository,

  // Code Repository
  CodeRepository,

  ProbeOwnerTeam,
  ProbeOwnerUser,

  AIAgent,
  AIAgentOwnerTeam,
  AIAgentOwnerUser,
  AIAgentTask,
  AIAgentTaskLog,
  AIAgentTaskPullRequest,
  AIAgentTaskTelemetryException,

  LlmProvider,
  LlmLog,

  UserSession,
  UserTotpAuth,
  UserWebAuthn,

  TelemetryIngestionKey,

  TelemetryException,

  TableView,

  // Dashboards
  Dashboard,
  DashboardDomain,

  MonitorTest,

  WorkspaceSetting,
  WorkspaceNotificationRule,
  WorkspaceNotificationSummary,

  MonitorFeed,

  MetricType,

  OnCallDutyPolicyTimeLog,

  ProjectSCIM,
  ProjectSCIMLog,
  StatusPageSCIMLog,

  KubernetesCluster,
];

const modelTypeMap: { [key: string]: { new (): BaseModel } } = {};

type GetModelTypeByNameFunction = (
  tableName: string,
) => { new (): BaseModel } | null;

export const getModelTypeByName: GetModelTypeByNameFunction = (
  tableName: string,
): { new (): BaseModel } | null => {
  if (modelTypeMap[tableName]) {
    return modelTypeMap[tableName] || null;
  }

  const modelType: { new (): BaseModel } | undefined = AllModelTypes.find(
    (modelType: { new (): BaseModel }) => {
      return new modelType().tableName === tableName;
    },
  );

  if (!modelType) {
    return null;
  }

  modelTypeMap[tableName] = modelType;

  return modelType;
};

export default AllModelTypes;
