# typed: true
class CreateInkbunnyInitialModels < ActiveRecord::Migration[7.0]
  def change
    # IB api reference:
    # https://wiki.inkbunny.net/wiki/API#Search

    create_table :domain_inkbunny_global_states do |t|
      t.string :key, null: false, unique: true
      t.string :value
      t.timestamps
    end

    create_table :domain_inkbunny_users do |t|
      t.integer :state, null: false
      t.json :state_detail

      t.string :name, null: false
      t.bigint :ib_user_id, null: false
      t.index :ib_user_id, unique: true

      # profile avatar (and log entry if there was one)
      t.string :avatar_url_str
      t.binary :avatar_file_sha256
      t.bigint :avatar_file_log_entry_id

      t.timestamps
    end

    create_table :domain_inkbunny_posts do |t|
      t.integer :state, null: false
      t.jsonb :state_detail

      t.datetime :shallow_updated_at
      t.datetime :deep_updated_at

      t.references :creator

      t.bigint :ib_post_id
      t.string :title
      t.string :description
      t.string :writing
      t.timestamp :posted_at
      t.timestamp :last_file_updated_at
      t.integer :rating
      t.integer :submission_type
      t.integer :num_views
      t.integer :num_files

      t.jsonb :ib_detail_raw

      t.timestamps
    end

    create_table :domain_inkbunny_files do |t|
      t.integer :state, null: false
      t.jsonb :state_detail, null: false, default: {}

      # no index on :post as we include it in the [:post, :file_order] idx
      t.references :post, null: false, index: false
      t.references :log_entry, index: false
      t.binary :blob_entry_sha256

      t.integer :file_order, null: false
      t.index %i[post_id file_order]

      t.bigint :ib_file_id
      t.jsonb :ib_detail_raw
      t.datetime :ib_created_at

      t.string :file_name
      t.string :url_str
      t.string :md5_initial
      t.string :md5_full
      t.jsonb :md5s

      t.timestamps
    end
    add_foreign_key :domain_inkbunny_files,
                    :domain_inkbunny_posts,
                    column: :post_id

    create_table :domain_inkbunny_pools do |t|
      t.string :name
      t.string :description
      t.timestamps
    end

    create_table :domain_inkbunny_pool_joins do |t|
      t.references :post, null: false
      t.references :pool, null: false
      t.integer :ordinal
    end
    add_foreign_key :domain_inkbunny_pool_joins,
                    :domain_inkbunny_posts,
                    column: :post_id
    add_foreign_key :domain_inkbunny_pool_joins,
                    :domain_inkbunny_pools,
                    column: :pool_id

    create_table :domain_inkbunny_favs, id: false do |t|
      t.references :user, null: false
      t.references :post, null: false
      t.index %i[user_id post_id], unique: true
      t.index %i[post_id user_id]
    end
    add_foreign_key :domain_inkbunny_favs,
                    :domain_inkbunny_users,
                    column: :user_id
    add_foreign_key :domain_inkbunny_favs,
                    :domain_inkbunny_posts,
                    column: :post_id

    create_table :domain_inkbunny_follows, id: false do |t|
      t.references :follower, null: false
      t.references :followed, null: false
      t.index %i[follower_id followed_id], unique: true
      t.index %i[followed_id follower_id]
    end
    add_foreign_key :domain_inkbunny_follows,
                    :domain_inkbunny_users,
                    column: :follower_id
    add_foreign_key :domain_inkbunny_follows,
                    :domain_inkbunny_users,
                    column: :followed_id

    create_table :domain_inkbunny_tags do |t|
      t.timestamps
    end

    create_table :domain_inkbunny_taggings do |t|
      t.timestamps
    end
  end
end
