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

  def up do
    create table(:application_settings, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :key, :string, null: false
      add :value, :text
      add :value_type, :string, null: false, default: "string"
      add :description, :text

      timestamps(type: :utc_datetime)
    end

    # Enforce unique keys
    create unique_index(:application_settings, [:key])

    # Insert default global cloud poller setting (nil = not configured)
    execute """
    INSERT INTO application_settings (id, key, value, value_type, description, inserted_at, updated_at)
    VALUES (
      gen_random_uuid(),
      'global_default_cloud_poller_id',
      NULL,
      'uuid',
      'Default cloud poller agent used as fallback when no organization-level agent is configured',
      NOW(),
      NOW()
    )
    """
  end

  def down do
    drop table(:application_settings)
  end
end
