# typed: false
class CreateTwitterTweets < ActiveRecord::Migration[7.0]
  def up
    # for twitter tables -
    # state - enum (ok, error)
    # state_detail - json, contents depends on state, grab-bag of debugging info
    # raw_data - json, more or less the raw pre-interpreted data surfaced
    #            from gallery-dl-server
    create_table :domain_twitter_users do |t|
      t.integer :state
      t.json :state_detail
      t.json :raw_data

      # twitter id associated with the user
      t.integer :tw_id
      t.index :tw_id, unique: true

      t.string :name, null: false
      t.string :nick
      t.string :description

      t.integer :num_fav_count
      t.integer :num_followers
      t.integer :num_following

      t.datetime :registered_at
      t.datetime :scanned_timeline_at
      t.timestamps
      t.index :name, unique: true
    end

    create_versions_table :domain_twitter_users

    create_table :domain_twitter_tweets do |t|
      t.integer :state
      t.json :state_detail
      t.json :raw_data

      t.references :author
      t.string :content
      t.references :reply_to_tweet # another tweet id

      t.timestamp :tweeted_at
      t.timestamps
    end

    add_foreign_key(
      :domain_twitter_tweets,
      :domain_twitter_users,
      column: :author_id,
      validate: true
    )

    create_table :domain_twitter_medias, id: false do |t|
      # id (primary key) - the filename from the twitter e.g. FjTpW15VEAA7FKz
      t.string :id, null: false
      t.index :id, unique: true

      t.integer :state
      t.json :state_detail
      t.json :raw_data

      # url of the file to request
      t.string :url_str
      # fk of the tweet this media belongs to
      t.references :tweet, null: false
      t.references :file # null file -> not yet downloaded

      t.timestamps
    end

    add_foreign_key(
      :domain_twitter_medias,
      :domain_twitter_tweets,
      column: :tweet_id,
      validate: true
    )
    add_foreign_key(
      :domain_twitter_medias,
      :http_log_entries,
      column: :file_id,
      validate: true
    )
  end

  def down
    raise("boom")
  end
end
