module Concernz

module AllocatableCinemaSection
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'allocatable_cinema_section_id'

    # AllocatableCinemaSection involves Cinema
    belongs_to :cinema, :foreign_key => :cinema_id

    validates :cinema_id, :presence => true
    validates :section_name, :presence => true
  end
end

end

module Concernz

module Booking
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'booking_id'

    # Booking involves Person
    belongs_to :person, :foreign_key => :person_id

    # Booking involves Session
    belongs_to :session, :foreign_key => :session_id

    # Booking is involved in Places Paid
    has_many :places_paids, :class_name => 'PlacesPaid', :foreign_key => :booking_id, :dependent => :destroy

    # Booking is involved in Seat Allocation
    has_many :seat_allocations, :class_name => 'SeatAllocation', :foreign_key => :booking_id, :dependent => :destroy
    has_many :seats, :through => :seat_allocations

    validates :booking_nr, :presence => true
    validates :number, :presence => true
    validates :person_id, :presence => true
    validates :session_id, :presence => true
  end
end

end

module Concernz

module Cinema
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'cinema_id'

    # Cinema is involved in AllocatableCinemaSection
    has_many :allocatable_cinema_sections, :class_name => 'AllocatableCinemaSection', :foreign_key => :cinema_id, :dependent => :destroy

    # Cinema contains Row and Row contains Seat
    has_many :seats_via_row, :class_name => 'Seat', :foreign_key => :row_cinema_id, :dependent => :destroy
    has_many :row_nrs, :through => :seats_via_row

    # Cinema is involved in Session
    has_many :sessions, :class_name => 'Session', :foreign_key => :cinema_id, :dependent => :destroy

    # Ticket Category is for Cinema
    has_many :ticket_categories, :class_name => 'TicketCategory', :foreign_key => :cinema_id, :dependent => :destroy

    validates :name, :presence => true
  end
end

end

module Concernz

module Film
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'film_id'

    # Film is involved in Session
    has_many :sessions, :class_name => 'Session', :foreign_key => :film_id, :dependent => :destroy

    validates :name, :presence => true
  end
end

end

module Concernz

module Person
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'person_id'

    # Person is involved in Booking
    has_many :bookings, :class_name => 'Booking', :foreign_key => :person_id, :dependent => :destroy
  end
end

end

module Concernz

module PlacesPaid
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'places_paid_id'

    # Places Paid involves Booking
    belongs_to :booking, :foreign_key => :booking_id

    validates :booking_id, :presence => true
    validates :number, :presence => true
    validates :payment_method_code, :presence => true
  end
end

end

module Concernz

module Seat
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'seat_id'

    # Seat is in Row and Row is in Cinema
    belongs_to :cinema_via_row, :class_name => 'Cinema', :foreign_key => :row_cinema_id

    # allocated-Seat is involved in Seat Allocation
    has_many :seat_allocations, :class_name => 'SeatAllocation', :foreign_key => :allocated_seat_id, :dependent => :destroy
    has_many :bookings, :through => :seat_allocations

    validates :row_cinema_id, :presence => true
    validates :row_nr, :presence => true
    validates :seat_number, :presence => true
  end
end

end

module Concernz

module SeatAllocation
  extend ActiveSupport::Concern
  included do
    # Seat Allocation involves Booking
    belongs_to :booking, :foreign_key => :booking_id

    # Seat Allocation involves allocated-Seat
    belongs_to :allocated_seat, :class_name => 'Seat', :foreign_key => :allocated_seat_id

    validates :booking_id, :presence => true
    validates :allocated_seat_id, :presence => true
  end
end

end

module Concernz

module Session
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'session_id'

    # Session involves Cinema
    belongs_to :cinema, :foreign_key => :cinema_id

    # Session involves Film
    belongs_to :film, :foreign_key => :film_id

    # Session is involved in Booking
    has_many :bookings, :class_name => 'Booking', :foreign_key => :session_id, :dependent => :destroy

    validates :cinema_id, :presence => true
    validates :film_id, :presence => true
    validates :session_time_year_nr, :presence => true
    validates :session_time_month_nr, :presence => true
    validates :session_time_day, :presence => true
    validates :session_time_hour, :presence => true
    validates :session_time_minute, :presence => true
  end
end

end

module Concernz

module TicketCategory
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'ticket_category_id'

    # Ticket Category is for Cinema
    belongs_to :cinema, :foreign_key => :cinema_id

    validates :cinema_id, :presence => true
    validates :section_name, :presence => true
    validates :session_time_year_nr, :presence => true
    validates :session_time_month_nr, :presence => true
    validates :session_time_day, :presence => true
    validates :session_time_hour, :presence => true
    validates :session_time_minute, :presence => true
  end
end

end