class SaleAudit < ApplicationRecord
  self.primary_key = "sale_audit_id"

  #primary key - need to be unique generated for SaleAudit
  before_create :generate_custom_id

  belongs_to :sale

  def self.sync_sale_audit_records(sale_audits)
    if !sale_audits.nil?
      sale_audits.each do |sa|
        sale_audit = SaleAudit.find_by_sale_audit_id(sa['sale_audit_id'])
        # unless SaleAudit.exists?(sa['sale_audit_id'])
        if sale_audit.nil?
          sale_audit = SaleAudit.new
        end
        sale_audit.sale_audit_id = sa['sale_audit_id']
        sale_audit.sale_id = sa['sale_id']
        sale_audit.action = sa['action']
        sale_audit.action_at = sa['action_at']
        sale_audit.action_by = sa['action_by']
        sale_audit.approved_by = sa['approved_by']
        sale_audit.approved_at = sa['approved_at']
        sale_audit.remark = sa['remark']
        sale_audit.save
      end
      Rails.logger.debug '....... Sale Audit sync completed .......'
    end
  end

  def self.record_audit_void(sale_id, void_by, approved_by, reason)
    #sale_audit
    sale_audit             = SaleAudit.new()
    sale_audit.sale_id     = sale_id
    sale_audit.action      = "SALEVOID"
    sale_audit.action_at   = DateTime.now.utc
    sale_audit.action_by   = void_by
    sale_audit.approved_by = approved_by
    sale_audit.remark      = reason
    sale_audit.save!
    #sale_audit.
  end

  def self.record_audit_complete(sale_id, remark, action_by)
    sale_audit             = SaleAudit.new()
    sale_audit.sale_id     = sale_id
    sale_audit.action      = "SALECOMPLETE"
    sale_audit.action_at   = DateTime.now.utc
    sale_audit.action_by   = Sale.find(sale_id).cashier_id
    sale_audit.remark      = remark
    sale_audit.approved_by = action_by
    sale_audit.save!
  end

  def self.record_audit_discount(sale_id, discount_by, approved_by, reason,type)
    #sale_audit
    sale_audit = SaleAudit.new()
    sale_audit.sale_id = sale_id
    sale_audit.action = type
    sale_audit.action_at = DateTime.now.utc
    sale_audit.action_by = discount_by
    sale_audit.approved_by = approved_by
    sale_audit.remark = reason
    sale_audit.save!
    #sale_audit.
  end

  def self.record_audit_foc(sale_id, cashier_id, approved_by, reason)
    #sale_audit
    sale_audit = SaleAudit.new()
    sale_audit.sale_id = sale_id
    sale_audit.action = "SALEFOC"
    sale_audit.action_at = DateTime.now.utc
    sale_audit.action_by = cashier_id
    sale_audit.approved_by = approved_by
    sale_audit.remark = reason
    sale_audit.save!
  end

  def self.record_audit_for_edit(sale_id, cashier_id, approved_by, reason,type)
    #sale_audit
    sale_audit             = SaleAudit.new()
    sale_audit.sale_id     = sale_id
    sale_audit.action      = type
    sale_audit.action_at   = DateTime.now.utc
    sale_audit.action_by   = cashier_id
    sale_audit.approved_by = approved_by
    sale_audit.remark      = reason
    sale_audit.save!
  end

  def self.record_payment(sale_id, remark, action_by)
    sale_audit = SaleAudit.new()
    sale_audit.sale_id = sale_id
    sale_audit.action = "SALEPAYMENT"
    sale_audit.action_at = DateTime.now.utc
    sale_audit.action_by = action_by
    sale_audit.remark = remark
    sale_audit.approved_by = action_by
    sale_audit.save!
  end

  def self.record_paymal(sale_id, remark, action_by)
    sale_audit = SaleAudit.new()
    sale_audit.sale_id = sale_id
    sale_audit.action = "PAYMAL"
    sale_audit.action_at = DateTime.now.utc
    sale_audit.action_by = Sale.find(sale_id).cashier_id
    sale_audit.remark = remark
    sale_audit.approved_by = action_by
    sale_audit.save!
  end

   def self.record_audit_sale(sale_id, remark, action_by,type)
    sale_audit = SaleAudit.new()
    sale_audit.sale_id = sale_id
    sale_audit.action = type
    sale_audit.action_at = DateTime.now.utc
    sale_audit.action_by = action_by
    sale_audit.remark = remark
    sale_audit.approved_by = action_by
    sale_audit.save!
  end

  def self.paymal_search(sale_id)
    amount = nil
    paymal = SaleAudit.where("sale_id =? and action = 'PAYMAL'",sale_id)
    if !paymal.nil?
      if paymal[0]
        remark = paymal[0].remark.split("}")
        response = "["+remark[0]+'}]'
        response = JSON.parse(response)
        puts response
        if response[0]["status"]  == true
          if response[0]["current_rebate_amount"].present?
            amount = response[0]["current_rebate_amount"]
          else
            amount = response[0]["current_balance_amount"]
          end
        else
          amount = nil
        end
      end
    end
     return amount

  end

  def self.getCardBalanceAmount(sale_id)
    card_balance_amount = 0
    sale_audits = SaleAudit.where("sale_id='#{sale_id}' AND action='PAYMAL'")
    if !sale_audits.nil?
      sale_audits.each do |sale_audit|
        if sale_audit.remark.split('}')[0]
          if self.valid_json('['+ sale_audit.remark.split('}')[0] + '}]')
            remark = JSON.parse(('['+ sale_audit.remark.split('}')[0] + '}]'))
            if remark[0]
              if remark[0]["status"]
                card_balance_amount = remark[0]["card_balance_amount"]
              end
            end
          end
        end
      end
    end
    
    return card_balance_amount
  end

  def self.valid_json(json)
      JSON.parse(json)
      return true
    rescue JSON::ParserError => e
      return false
  end

  private
    def generate_custom_id
      self.sale_audit_id = SeedGenerator.generate_id(self.class.name, "SAI")
    end
end
