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

  def up do
    # Revert mikrotik_username from encrypted binary to plaintext string
    # Usernames are not sensitive enough to require encryption (passwords still encrypted)
    # This fixes form display issues with encrypted binary fields

    # Organizations
    execute "ALTER TABLE organizations ALTER COLUMN mikrotik_username TYPE varchar USING NULL"

    # Sites
    execute "ALTER TABLE sites ALTER COLUMN mikrotik_username TYPE varchar USING NULL"

    # Devices
    execute "ALTER TABLE devices ALTER COLUMN mikrotik_username TYPE varchar USING NULL"
  end

  def down do
    # Change back to binary for encryption
    # Note: This will clear any existing usernames

    # Organizations
    execute "ALTER TABLE organizations ALTER COLUMN mikrotik_username TYPE bytea USING NULL"

    # Sites
    execute "ALTER TABLE sites ALTER COLUMN mikrotik_username TYPE bytea USING NULL"

    # Devices
    execute "ALTER TABLE devices ALTER COLUMN mikrotik_username TYPE bytea USING NULL"
  end
end
