defmodule Towerops.Repo.Migrations.RenameEquipmentToDevices do
  use Ecto.Migration

  def up do
    # Rename the main table
    rename table(:equipment), to: table(:devices)

    # Rename foreign key columns in related tables
    rename table(:alerts), :equipment_id, to: :device_id
    rename table(:snmp_devices), :equipment_id, to: :device_id
    rename table(:monitoring_checks), :equipment_id, to: :device_id
    rename table(:equipment_events), :equipment_id, to: :device_id
    rename table(:agent_assignments), :equipment_id, to: :device_id
    rename table(:snmp_neighbors), :equipment_id, to: :device_id

    # Rename equipment_events table
    rename table(:equipment_events), to: table(:device_events)

    # Rename indexes (they get renamed automatically with the table, but foreign key indexes need explicit handling)
    execute "ALTER INDEX IF EXISTS equipment_pkey RENAME TO devices_pkey"
    execute "ALTER INDEX IF EXISTS equipment_site_id_index RENAME TO devices_site_id_index"

    execute "ALTER INDEX IF EXISTS equipment_monitoring_enabled_index RENAME TO devices_monitoring_enabled_index"

    execute "ALTER INDEX IF EXISTS equipment_snmp_enabled_monitoring_enabled_site_id_index RENAME TO devices_snmp_enabled_monitoring_enabled_site_id_index"

    # Rename foreign key constraints
    execute "ALTER TABLE alerts DROP CONSTRAINT IF EXISTS alerts_equipment_id_fkey"

    execute "ALTER TABLE alerts ADD CONSTRAINT alerts_device_id_fkey FOREIGN KEY (device_id) REFERENCES devices(id) ON DELETE CASCADE"

    execute "ALTER TABLE snmp_devices DROP CONSTRAINT IF EXISTS snmp_devices_equipment_id_fkey"

    execute "ALTER TABLE snmp_devices ADD CONSTRAINT snmp_devices_device_id_fkey FOREIGN KEY (device_id) REFERENCES devices(id) ON DELETE CASCADE"

    execute "ALTER TABLE monitoring_checks DROP CONSTRAINT IF EXISTS monitoring_checks_equipment_id_fkey"

    execute "ALTER TABLE monitoring_checks ADD CONSTRAINT monitoring_checks_device_id_fkey FOREIGN KEY (device_id) REFERENCES devices(id) ON DELETE CASCADE"

    execute "ALTER TABLE device_events DROP CONSTRAINT IF EXISTS equipment_events_equipment_id_fkey"

    execute "ALTER TABLE device_events ADD CONSTRAINT device_events_device_id_fkey FOREIGN KEY (device_id) REFERENCES devices(id) ON DELETE CASCADE"

    execute "ALTER TABLE agent_assignments DROP CONSTRAINT IF EXISTS agent_assignments_equipment_id_fkey"

    execute "ALTER TABLE agent_assignments ADD CONSTRAINT agent_assignments_device_id_fkey FOREIGN KEY (device_id) REFERENCES devices(id) ON DELETE CASCADE"

    execute "ALTER TABLE snmp_neighbors DROP CONSTRAINT IF EXISTS snmp_neighbors_equipment_id_fkey"

    execute "ALTER TABLE snmp_neighbors ADD CONSTRAINT snmp_neighbors_device_id_fkey FOREIGN KEY (device_id) REFERENCES devices(id) ON DELETE CASCADE"

    # Rename composite indexes
    execute "ALTER INDEX IF EXISTS agent_assignments_agent_token_id_equipment_id_index RENAME TO agent_assignments_agent_token_id_device_id_index"

    execute "ALTER INDEX IF EXISTS agent_assignments_equipment_id_index RENAME TO agent_assignments_device_id_index"

    execute "ALTER INDEX IF EXISTS agent_assignments_equipment_id_enabled_index RENAME TO agent_assignments_device_id_enabled_index"

    execute "ALTER INDEX IF EXISTS alerts_equipment_id_severity_status_index RENAME TO alerts_device_id_severity_status_index"
  end

  def down do
    # Reverse all changes
    rename table(:devices), to: table(:equipment)
    rename table(:alerts), :device_id, to: :equipment_id
    rename table(:snmp_devices), :device_id, to: :equipment_id
    rename table(:monitoring_checks), :device_id, to: :equipment_id
    rename table(:device_events), :device_id, to: :equipment_id
    rename table(:agent_assignments), :device_id, to: :equipment_id
    rename table(:snmp_neighbors), :device_id, to: :equipment_id
    rename table(:device_events), to: table(:equipment_events)

    execute "ALTER INDEX IF EXISTS devices_pkey RENAME TO equipment_pkey"
    execute "ALTER INDEX IF EXISTS devices_site_id_index RENAME TO equipment_site_id_index"

    execute "ALTER INDEX IF EXISTS devices_monitoring_enabled_index RENAME TO equipment_monitoring_enabled_index"

    execute "ALTER INDEX IF EXISTS devices_snmp_enabled_monitoring_enabled_site_id_index RENAME TO equipment_snmp_enabled_monitoring_enabled_site_id_index"

    execute "ALTER TABLE alerts DROP CONSTRAINT IF EXISTS alerts_device_id_fkey"

    execute "ALTER TABLE alerts ADD CONSTRAINT alerts_equipment_id_fkey FOREIGN KEY (equipment_id) REFERENCES equipment(id) ON DELETE CASCADE"

    execute "ALTER TABLE snmp_devices DROP CONSTRAINT IF EXISTS snmp_devices_device_id_fkey"

    execute "ALTER TABLE snmp_devices ADD CONSTRAINT snmp_devices_equipment_id_fkey FOREIGN KEY (equipment_id) REFERENCES equipment(id) ON DELETE CASCADE"

    execute "ALTER TABLE monitoring_checks DROP CONSTRAINT IF EXISTS monitoring_checks_device_id_fkey"

    execute "ALTER TABLE monitoring_checks ADD CONSTRAINT monitoring_checks_equipment_id_fkey FOREIGN KEY (equipment_id) REFERENCES equipment(id) ON DELETE CASCADE"

    execute "ALTER TABLE equipment_events DROP CONSTRAINT IF EXISTS device_events_device_id_fkey"

    execute "ALTER TABLE equipment_events ADD CONSTRAINT equipment_events_equipment_id_fkey FOREIGN KEY (equipment_id) REFERENCES equipment(id) ON DELETE CASCADE"

    execute "ALTER TABLE agent_assignments DROP CONSTRAINT IF EXISTS agent_assignments_device_id_fkey"

    execute "ALTER TABLE agent_assignments ADD CONSTRAINT agent_assignments_equipment_id_fkey FOREIGN KEY (equipment_id) REFERENCES equipment(id) ON DELETE CASCADE"

    execute "ALTER TABLE snmp_neighbors DROP CONSTRAINT IF EXISTS snmp_neighbors_device_id_fkey"

    execute "ALTER TABLE snmp_neighbors ADD CONSTRAINT snmp_neighbors_equipment_id_fkey FOREIGN KEY (equipment_id) REFERENCES equipment(id) ON DELETE CASCADE"

    execute "ALTER INDEX IF EXISTS agent_assignments_agent_token_id_device_id_index RENAME TO agent_assignments_agent_token_id_equipment_id_index"

    execute "ALTER INDEX IF EXISTS alerts_device_id_severity_status_index RENAME TO alerts_equipment_id_severity_status_index"
  end
end
