# typed: strict

class AddAuxTablesForDomainUsersFaUsers < ActiveRecord::Migration[7.2]
  sig { void }
  def change
    mirai_tablespace!

    create_aux_table(:domain_users, :fa) do |t|
      t.string :state
      t.string :name
      t.string :url_name
      t.string :full_name
      t.string :account_status
      t.string :artist_type
      t.string :mood
      t.string :profile_html
      # # num_* are those indicated on the user page
      t.integer :num_pageviews
      t.integer :num_submissions
      t.integer :num_comments_recieved
      t.integer :num_comments_given
      t.integer :num_journals
      t.integer :num_favorites
      t.integer :num_watched_by
      t.integer :num_watching
      t.datetime :scanned_gallery_at
      t.datetime :scanned_page_at
      t.datetime :scanned_follows_at
      t.datetime :scanned_followed_by_at
      t.datetime :scanned_incremental_at
      t.datetime :registered_at
      t.datetime :migrated_followed_users_at
      t.references :last_user_page, foreign_key: { to_table: :http_log_entries }
      t.references :last_gallery_page,
                   foreign_key: {
                     to_table: :http_log_entries,
                   }
      t.string :page_scan_error
      t.boolean :is_disabled
    end

    up_only { execute <<-SQL }
    INSERT INTO domain_users_fa_aux (
      base_table_id,
      state,
      name,
      url_name,
      full_name,
      account_status,
      artist_type,
      mood,
      profile_html,
      num_pageviews,
      num_submissions,
      num_comments_recieved,
      num_comments_given,
      num_journals,
      num_favorites,
      num_watched_by,
      num_watching,
      scanned_gallery_at,
      scanned_page_at,
      scanned_follows_at,
      scanned_followed_by_at,
      scanned_incremental_at,
      registered_at,
      migrated_followed_users_at,
      last_user_page_id,
      last_gallery_page_id,
      page_scan_error,
      is_disabled
    )
    SELECT
      id as base_table_id,
      (json_attributes->>'state')::text as state,
      (json_attributes->>'name')::text as name,
      (json_attributes->>'url_name')::text as url_name,
      (json_attributes->>'full_name')::text as full_name,
      (json_attributes->>'account_status')::text as account_status,
      (json_attributes->>'artist_type')::text as artist_type,
      (json_attributes->>'mood')::text as mood,
      (json_attributes->>'profile_html')::text as profile_html,
      (json_attributes->>'num_pageviews')::integer as num_pageviews,
      (json_attributes->>'num_submissions')::integer as num_submissions,
      (json_attributes->>'num_comments_recieved')::integer as num_comments_recieved,
      (json_attributes->>'num_comments_given')::integer as num_comments_given,
      (json_attributes->>'num_journals')::integer as num_journals,
      (json_attributes->>'num_favorites')::integer as num_favorites,
      (json_attributes->>'num_watched_by')::integer as num_watched_by,
      (json_attributes->>'num_watching')::integer as num_watching,
      (json_attributes->>'scanned_gallery_at')::timestamp as scanned_gallery_at,
      (json_attributes->>'scanned_page_at')::timestamp as scanned_page_at,
      (json_attributes->>'scanned_follows_at')::timestamp as scanned_follows_at,
      (json_attributes->>'scanned_followed_by_at')::timestamp as scanned_followed_by_at,
      (json_attributes->>'scanned_incremental_at')::timestamp as scanned_incremental_at,
      (json_attributes->>'registered_at')::timestamp as registered_at,
      (json_attributes->>'migrated_followed_users_at')::timestamp as migrated_followed_users_at,
      (json_attributes->>'last_user_page_id')::integer as last_user_page_id,
      (json_attributes->>'last_gallery_page_id')::integer as last_gallery_page_id,
      (json_attributes->>'page_scan_error')::text as page_scan_error,
      (json_attributes->>'is_disabled')::boolean as is_disabled
    FROM domain_users
    WHERE type = 'Domain::User::FaUser'
    SQL
  end
end
