class AssignedOrderItem < ApplicationRecord
  before_create :generate_custom_id

  #primary key - need to be unique
  self.primary_key = "assigned_order_item_id"

  belongs_to :order
  belongs_to :order_queue_station

  def self.sync_assigned_order_item_records(assigned_order_items)
    if !assigned_order_items.nil?
      assigned_order_items.each do |item|
        order_item = AssignedOrderItem.find_by_assigned_order_item_id(item['assigned_order_item_id'])
        # unless AssignedOrderItem.exists?(item['assigned_order_item_id'])
        if order_item.nil?
          order_item = AssignedOrderItem.new
        end
        order_item.assigned_order_item_id = item['assigned_order_item_id']
        order_item.item_code = item['item_code']
        order_item.instance_code = item['instance_code']
        order_item.order_queue_station_id = item['order_queue_station_id']
        order_item.order_id = item['order_id']
        order_item.print_status = item['print_status']
        order_item.delivery_status = item['delivery_status']
        order_item.save
      end
      Rails.logger.debug '....... Assigned Order Item sync completed .......'
    end
  end

  def self.assigned_order_item (order, item_code, instance_code, order_queue_station )
      assigned_order_item = AssignedOrderItem.new()
      assigned_order_item.order = order
      assigned_order_item.item_code = item_code
      assigned_order_item.instance_code = instance_code
      assigned_order_item.order_queue_station = order_queue_station
      assigned_order_item.print_status = false
      assigned_order_item.delivery_status = false
      assigned_order_item.save!
  end

  def self.assigned_order_item_by_job(order_id)
    order_item = AssignedOrderItem.select("assigned_order_items.assigned_order_item_id,
                              assigned_order_items.order_queue_station_id, 
                              oqs.id as station_id, oqs.station_name, 
                              oqs.is_active, oqpz.zone_id, 
                              df.name as zone, df.type as table_type, 
                              odt.order_id, odt.item_code, odt.item_instance_code, odt.item_name, 
                              odt.price, odt.qty, odt.item_order_by, odt.options, 
                              cus.name as customer_name, odt.created_at")
    .joins(" left join order_queue_stations as oqs on oqs.id = assigned_order_items.order_queue_station_id
              left join order_queue_process_by_zones as oqpz on oqpz.order_queue_station_id = oqs.id
              left join orders as od ON od.order_id = assigned_order_items.order_id
              left join order_items as odt ON odt.item_code = assigned_order_items.item_code AND odt.order_id = assigned_order_items.order_id
              left join customers as cus ON cus.customer_id = od.customer_id
              left join booking_orders as bo on bo.order_id = assigned_order_items.order_id
              left join bookings as bk on bk.booking_id = bo.booking_id
              left join dining_facilities as df on df.id = bk.dining_facility_id")
    .where("assigned_order_items.order_id = '#{order_id}'  AND assigned_order_items.delivery_status = false AND assigned_order_items.created_at >= '#{Time.now.beginning_of_day.utc}' ")
    .order("assigned_order_items.assigned_order_item_id desc")
    .group("assigned_order_items.assigned_order_item_id")
    return order_item
  end

  private
  def generate_custom_id
    self.assigned_order_item_id = SeedGenerator.generate_id(self.class.name, "AOI")
  end
end
