module Concernz

module Country
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'country_id'

    # Diplomat represents Country
    has_many :diplomats, :class_name => 'Diplomat', :foreign_key => :represented_country_id, :dependent => :destroy

    # Diplomat serves in Country
    has_many :diplomats, :class_name => 'Diplomat', :foreign_key => :served_country_id, :dependent => :destroy

    # Country is involved in LanguageUse
    has_many :language_uses, :class_name => 'LanguageUse', :foreign_key => :country_id, :dependent => :destroy
    has_many :languages, :through => :language_uses

    # Country is involved in Representation
    has_many :representations, :class_name => 'Representation', :foreign_key => :country_id, :dependent => :destroy

    # Country is involved in Representation
    has_many :representations, :class_name => 'Representation', :foreign_key => :represented_country_id, :dependent => :destroy

    validates :country_name, :presence => true
  end
end

end

module Concernz

module Diplomat
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'diplomat_id'

    # Diplomat represents Country
    belongs_to :represented_country, :class_name => 'Country', :foreign_key => :represented_country_id

    # Diplomat serves in Country
    belongs_to :served_country, :class_name => 'Country', :foreign_key => :served_country_id

    # Diplomat is involved in Fluency
    has_many :fluencies, :class_name => 'Fluency', :foreign_key => :diplomat_id, :dependent => :destroy
    has_many :languages, :through => :fluencies

    # Diplomat is an Ambassador and Ambassador is involved in Representation
    has_many :representations_via_ambassador, :class_name => 'Representation', :foreign_key => :ambassador_id, :dependent => :destroy

    validates :diplomat_name, :presence => true
    validates :represented_country_id, :presence => true
    validates :served_country_id, :presence => true
  end
end

end

module Concernz

module Fluency
  extend ActiveSupport::Concern
  included do
    # Fluency involves Diplomat
    belongs_to :diplomat, :foreign_key => :diplomat_id

    # Fluency involves Language
    belongs_to :language, :foreign_key => :language_id

    validates :diplomat_id, :presence => true
    validates :language_id, :presence => true
  end
end

end

module Concernz

module Language
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'language_id'

    # Language is involved in Fluency
    has_many :fluencies, :class_name => 'Fluency', :foreign_key => :language_id, :dependent => :destroy
    has_many :diplomats, :through => :fluencies

    # Language is involved in LanguageUse
    has_many :language_uses, :class_name => 'LanguageUse', :foreign_key => :language_id, :dependent => :destroy
    has_many :countries, :through => :language_uses

    validates :language_name, :presence => true
  end
end

end

module Concernz

module LanguageUse
  extend ActiveSupport::Concern
  included do
    # LanguageUse involves Country
    belongs_to :country, :foreign_key => :country_id

    # LanguageUse involves Language
    belongs_to :language, :foreign_key => :language_id

    validates :language_id, :presence => true
    validates :country_id, :presence => true
  end
end

end

module Concernz

module Representation
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'representation_id'

    # Representation involves Country
    belongs_to :country, :foreign_key => :country_id

    # Representation involves Country
    belongs_to :represented_country, :class_name => 'Country', :foreign_key => :represented_country_id

    # Representation involves Ambassador and Ambassador is a kind of Diplomat
    belongs_to :diplomat_via_ambassador, :class_name => 'Diplomat', :foreign_key => :ambassador_id

    validates :ambassador_id, :presence => true
    validates :country_id, :presence => true
    validates :represented_country_id, :presence => true
  end
end

end