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

  def change do
    create table(:gaiia_accounts, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :organization_id, references(:organizations, type: :binary_id, on_delete: :delete_all),
        null: false

      add :gaiia_id, :string, null: false
      add :readable_id, :string
      add :name, :string
      add :status, :string
      add :account_type, :string
      add :address, :map
      add :subscription_count, :integer
      add :mrr, :decimal
      add :raw_data, :map

      timestamps(type: :utc_datetime)
    end

    create unique_index(:gaiia_accounts, [:organization_id, :gaiia_id])
    create index(:gaiia_accounts, [:organization_id])

    create table(:gaiia_network_sites, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :organization_id, references(:organizations, type: :binary_id, on_delete: :delete_all),
        null: false

      add :gaiia_id, :string, null: false
      add :name, :string
      add :address, :map
      add :ip_blocks, {:array, :string}, default: []
      add :account_count, :integer
      add :total_mrr, :decimal
      add :site_id, references(:sites, type: :binary_id, on_delete: :nilify_all)
      add :raw_data, :map

      timestamps(type: :utc_datetime)
    end

    create unique_index(:gaiia_network_sites, [:organization_id, :gaiia_id])
    create index(:gaiia_network_sites, [:organization_id])
    create index(:gaiia_network_sites, [:site_id])

    create table(:gaiia_inventory_items, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :organization_id, references(:organizations, type: :binary_id, on_delete: :delete_all),
        null: false

      add :gaiia_id, :string, null: false
      add :name, :string
      add :status, :string
      add :serial_number, :string
      add :mac_address, :string
      add :ip_address, :string
      add :model_name, :string
      add :manufacturer_name, :string
      add :category, :string
      add :assigned_account_gaiia_id, :string
      add :assigned_network_site_gaiia_id, :string
      add :device_id, references(:devices, type: :binary_id, on_delete: :nilify_all)
      add :raw_data, :map

      timestamps(type: :utc_datetime)
    end

    create unique_index(:gaiia_inventory_items, [:organization_id, :gaiia_id])
    create index(:gaiia_inventory_items, [:organization_id])
    create index(:gaiia_inventory_items, [:device_id])

    create table(:gaiia_billing_subscriptions, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :organization_id, references(:organizations, type: :binary_id, on_delete: :delete_all),
        null: false

      add :gaiia_id, :string, null: false
      add :account_gaiia_id, :string
      add :status, :string
      add :product_name, :string
      add :mrr_amount, :decimal
      add :currency, :string
      add :speed_download, :integer
      add :speed_upload, :integer
      add :raw_data, :map

      timestamps(type: :utc_datetime)
    end

    create unique_index(:gaiia_billing_subscriptions, [:organization_id, :gaiia_id])
    create index(:gaiia_billing_subscriptions, [:organization_id])
    create index(:gaiia_billing_subscriptions, [:account_gaiia_id])
  end
end
