#!/usr/bin/env ruby

require "bundler/setup"
require_relative "lib/has_aux_table"
Bundler.require(:default, :development)

ActiveRecord::Base.establish_connection(
  adapter: "sqlite3",
  database: ":memory:"
)
# ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  # Create base table and aux table in the same block
  create_base_table :users do |t|
    t.create_aux :fa do |t|
      t.string :username, index: true
      t.string :url_name, index: true
    end

    t.timestamps
  end

  # Change base table later to add an aux table
  change_base_table :users do |t|
    t.create_aux :e621 do |t|
      t.integer :e621_id, index: true
      t.string :username, index: true
    end
  end

  create_base_table :posts do |t|
    t.timestamp :posted_at, index: true
    t.timestamps
  end

  # Directly create aux tables
  create_aux_table :posts, :fa do |t|
    t.references :creator,
                 foreign_key: {
                   to_table: :users_fa_aux,
                   primary_key: :base_table_id
                 }
    t.integer :fa_id, index: true
    t.string :title
    t.string :species
  end

  create_aux_table :posts, :e621 do |t|
    t.references :creator,
                 foreign_key: {
                   to_table: :users_e621_aux,
                   primary_key: :base_table_id
                 }
    t.integer :e621_id, index: true
    t.string :md5, index: true
  end

  create_table :user_post_fav_joins, id: false do |t|
    t.references :user, null: false, foreign_key: { to_table: :users }
    t.references :post, null: false, foreign_key: { to_table: :posts }
    t.timestamps
  end
end

class UserPostFavJoin < ActiveRecord::Base
  belongs_to :user
  belongs_to :post
end

class User < ActiveRecord::Base
  include HasAuxTable
  has_many :user_post_fav_joins, dependent: :destroy
  has_many :favorite_posts, through: :user_post_fav_joins, source: :post
  has_many :created_posts, class_name: "Post", inverse_of: :creator
end

class FaUser < User
  aux_table :fa
  validates :url_name, presence: true
  has_many :created_posts, class_name: "FaPost", inverse_of: :creator
end

class E621User < User
  aux_table :e621
  validates :username, presence: true
  has_many :created_posts, class_name: "E621Post", inverse_of: :creator
end

class Post < ActiveRecord::Base
  include HasAuxTable
  has_many :user_post_fav_joins, dependent: :destroy
  has_many :favoriting_users, through: :user_post_fav_joins, source: :user
  validates_presence_of :posted_at
end

class FaPost < Post
  aux_table :fa
  belongs_to :creator, class_name: "FaUser", inverse_of: :created_posts
  validates :fa_id, presence: true, uniqueness: true
  validates :title, presence: true
end

class E621Post < Post
  aux_table :e621
  validates :e621_id, presence: true
  belongs_to :creator, class_name: "E621User", inverse_of: :created_posts
end

puts FaPost.inspect

fa_user = FaUser.create!(username: "Alice", url_name: "alice")
fa_user_id = fa_user.id
raise if fa_user.id.nil?
raise unless fa_user.persisted?
raise unless fa_user.username == "Alice"
raise unless fa_user.url_name == "alice"

fa_user_found = FaUser.find_by(username: "Alice")
raise unless fa_user_found.id == fa_user_id
raise unless fa_user_found.username == "Alice"
raise unless fa_user_found.url_name == "alice"

fa_user_found = FaUser.find(fa_user_id)
raise unless fa_user_found.id == fa_user_id
raise unless fa_user_found.username == "Alice"
raise unless fa_user_found.url_name == "alice"

fa_post =
  fa_user.created_posts.create!(
    fa_id: 12_345,
    title: "Test Post",
    species: "Cat",
    posted_at: 1.day.ago
  )
raise unless fa_post.persisted?
raise unless fa_post.creator == fa_user
raise unless fa_post.creator_id == fa_user.id

fa_posts_all = FaPost.all.to_a
raise unless fa_posts_all.size == 1
raise unless fa_posts_all.first.creator == fa_user
raise unless fa_posts_all.first.creator_id == fa_user.id

raise unless FaPost.exists?(fa_id: 12_345)
raise if FaPost.exists?(fa_id: 12_346)

e621_user = E621User.create!(username: "bob", e621_id: 67_890)
raise unless e621_user.persisted?
raise unless e621_user.username == "bob"
raise unless e621_user.e621_id == 67_890

e621_user_found = E621User.find_by(username: "bob")
raise unless e621_user_found.id == e621_user.id
raise unless e621_user_found.username == "bob"
raise unless e621_user_found.e621_id == 67_890

e621_post =
  e621_user.created_posts.create!(
    e621_id: 102_938,
    md5: "DEADBEEF" * 4,
    posted_at: 2.weeks.ago
  )
raise unless e621_post.persisted?
raise unless e621_post.creator == e621_user
raise unless e621_post.creator_id == e621_user.id

e621_user.favorite_posts << e621_post
raise unless e621_user.favorite_posts.size == 1
raise unless e621_user.favorite_posts.first == e621_post
raise unless e621_user.favorite_posts.first.id == e621_post.id

e621_fav_joins = e621_user.user_post_fav_joins
raise unless e621_fav_joins.size == 1
raise unless e621_fav_joins.first.user == e621_user
raise unless e621_fav_joins.first.post == e621_post
raise unless e621_fav_joins.first.post_id == e621_post.id

e621_posts_all = E621Post.all.to_a
raise unless e621_posts_all.size == 1
raise unless e621_posts_all.first.creator == e621_user
raise unless e621_posts_all.first.creator_id == e621_user.id
