-- Veeam Backup Agents (agent installs on managed machines)
CREATE TABLE IF NOT EXISTS veeam_backup_agents (
  instance_uid VARCHAR(255) PRIMARY KEY,
  organization_uid VARCHAR(255) REFERENCES veeam_organizations(instance_uid) ON DELETE SET NULL,
  site_uid VARCHAR(255),
  management_agent_uid VARCHAR(255),
  name VARCHAR(500) NOT NULL,
  agent_platform VARCHAR(50),
  status VARCHAR(50),
  management_agent_status VARCHAR(50),
  operation_mode VARCHAR(50),
  gui_mode VARCHAR(50),
  platform VARCHAR(50),
  version VARCHAR(100),
  version_status VARCHAR(50),
  management_mode VARCHAR(100),
  installation_type VARCHAR(50),
  activation_time TIMESTAMPTZ,
  total_jobs_count INTEGER DEFAULT 0,
  running_jobs_count INTEGER DEFAULT 0,
  success_jobs_count INTEGER DEFAULT 0,
  synced_at TIMESTAMPTZ DEFAULT NOW(),
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Veeam VSPC Alarms (platform-level alarms, distinct from job failures)
CREATE TABLE IF NOT EXISTS veeam_alarms (
  instance_uid VARCHAR(255) PRIMARY KEY,
  alarm_template_uid VARCHAR(255),
  repeat_count INTEGER DEFAULT 0,
  object_uid VARCHAR(255),
  object_type VARCHAR(100),
  object_name VARCHAR(500),
  object_computer_name VARCHAR(500),
  organization_uid VARCHAR(255) REFERENCES veeam_organizations(instance_uid) ON DELETE SET NULL,
  location_uid VARCHAR(255),
  management_agent_uid VARCHAR(255),
  last_activation_uid VARCHAR(255),
  last_activation_time TIMESTAMPTZ,
  last_activation_status VARCHAR(50),
  last_activation_message TEXT,
  last_activation_remark TEXT,
  area VARCHAR(50),
  resolved BOOLEAN DEFAULT false,
  synced_at TIMESTAMPTZ DEFAULT NOW(),
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_veeam_agents_org ON veeam_backup_agents(organization_uid);
CREATE INDEX IF NOT EXISTS idx_veeam_agents_status ON veeam_backup_agents(status);
CREATE INDEX IF NOT EXISTS idx_veeam_agents_mgmt_status ON veeam_backup_agents(management_agent_status);
CREATE INDEX IF NOT EXISTS idx_veeam_agents_platform ON veeam_backup_agents(agent_platform);
CREATE INDEX IF NOT EXISTS idx_veeam_agents_synced ON veeam_backup_agents(synced_at);

CREATE INDEX IF NOT EXISTS idx_veeam_alarms_org ON veeam_alarms(organization_uid);
CREATE INDEX IF NOT EXISTS idx_veeam_alarms_status ON veeam_alarms(last_activation_status);
CREATE INDEX IF NOT EXISTS idx_veeam_alarms_resolved ON veeam_alarms(resolved);
CREATE INDEX IF NOT EXISTS idx_veeam_alarms_time ON veeam_alarms(last_activation_time);
CREATE INDEX IF NOT EXISTS idx_veeam_alarms_synced ON veeam_alarms(synced_at);
