class GetAllIDCardDataByPlanTPAQuery

  def initialize(pl_plan_key)
    @pl_plan_key = ActiveRecord::Base.connection.quote(pl_plan_key)
  end

  def call
    VhcsRecord.connection.exec_query(sql).rows
  end

  def to_id_cards_data
    raw_data = call

    all_id_cards_data = []
    raw_data.each do |rd|
        id_card_data = {
            id: rd[0],
            mb_member_key: rd[1],
            mbr_type: rd[2],
            division: rd[3],
            full_name: rd[7],
            family_id: rd[8],
            mbr_class: rd[9],
            effect_date: rd[11],
            state: rd[12],
            pl_plan_key: rd[14],
            group_number: rd[15],
            medical_group_num: rd[16],
            dental_group_num: rd[17],
            pb_product_key: rd[18],
            mb_member_key: rd[19]
        }

        all_id_cards_data.push(id_card_data)
    end

    all_id_cards_data
  end

  private

  def sql
    <<~SQL
      SELECT DISTINCT ROW_NUMBER() OVER(ORDER BY pbm.FamilyId ASC) as Id, pbc.MBMemberKey,      	
            CASE WHEN div.PBENtityKey = 344215 OR div.PBENtityKey = 344217 THEN 'OSB' ELSE  ou.CompanyNumber END as MbrType, 
            substring(div.LastName,0,41) as Div ,
            substring(pbe.FirstName,0,25) as MbrFirst, 
            substring(pbe.MiddleName,0,25) as MbrMiddle,    
            substring(pbe.LastName,0,50) as MbrLast, 
            (substring(pbe.FirstName, 0, 25) + ' ' + substring(pbe.LastName, 0, 50)) as FullName,
            pbm.FamilyId, 
            UPPER(lct.shortdesc) as  MbrClass,          
            2 as MedOrDent,pp.InEffect as EffectDate,
            pbea.State,ISNULL(oua.State,pbea.State) as EmpState,
            M.PLPlanKey,
            PC.GroupNumber,
            PC.MedicalNumber,
            PC.DentalNumber,
            P.PBProductKey,
            m.PBEntityKey as  PrimaryMBMemberKey
        FROM  VWMBMember M    	INNER JOIN PBEntityClaimsData pbc on pbc.MBMemberKey=M.MBMemberKey
        LEFT OUTER Join HLPlanCode PC ON PC.PlanKey = M.PLPlanKey 
        INNER JOIN PBMember pbm on pbm.PBEntityKey=pbc.PBEntityKey and pbm.EnrolleeTypeKey=1044
        INNER JOIN PBEntity pbe on pbe.PBEntityKey=pbm.PBEntityKey
        INNER JOIN PBEntityAddress pbea on pbea.PBEntityKey=pbe.PBEntityKey and pbea.AddressTypeID=1137
        INNER JOIN PBCoveredEntities ce on ce.PBEntityKey=pbe.PBEntityKey
        INNER JOIN PBProductParticipation pp on pp.PBProductParticipationKey=ce.PBProductParticipationKey
        INNER JOIN (SELECT ce1.PBEntityKey, pp1.PBProductAvailabilityKey, MAX(pp1.WhenLastChanged) as WhenLastChanged,
                    pp1.CoverageTypeCode          
                    FROM PBCoveredEntities ce1
                    INNER JOIN PBProductParticipation pp1 on pp1.PBProductParticipationKey = ce1.PBProductParticipationKey
                    GROUP BY ce1.PBEntityKey, pp1.PBProductAvailabilityKey,pp1.CoverageTypeCode) 
                    cepp ON cepp.PBEntityKey = ce.PBEntityKey AND cepp.PBProductAvailabilityKey = pp.PBProductAvailabilityKey
                    and cepp.CoverageTypeCode = pp.CoverageTypeCode
        INNER JOIN GEN_LookupTables  lct ON lct.RecordID = pp.CoverageTypeCode
        INNER JOIN PBProductAvailability pa on pa.PBProductAvailabilityKey=pp.PBProductAvailabilityKey
        INNER JOIN PBProduct p on p.PBProductKey=pa.PBProductKey
        INNER JOIN PBAffiliation          aff ON aff.PBAffiliationKey = pp.PBAffiliationKey
        INNER JOIN PBEntity div on div.PBEntityKey=aff.ParentPBEntityKey
        LEFT OUTER JOIN PBOrgUnit ou on ou.PBEntityKey=div.PBEntityKey
        LEFT OUTER JOIN PBEntityAddress oua on oua.PBEntityKey=ou.PBEntityKey and oua.AddressTypeID=1137    	WHERE p.PBProductKey in (Select DISTINCT PBProductKey From PBProduct Where FullDescription NOT LIKE '%LIFE%' AND FullDescription NOT LIKE '%VIS%') 
            AND pp.InEffect <= getdate() +90
            AND (pp.OutOfEffect-1 > getdate() AND 
            pp.OutOfEffect>pp.InEffect)
            AND pp.OutOfEffect=(Select Max(Part.OutOfEffect) From PBProductParticipation Part
                                INNER JOIN PBProductAvailability Aval on Aval.PBProductAvailabilityKey=Part.PBProductAvailabilityKey
                                INNER JOIN PBProduct Prod on Prod.PBProductKey=Aval.PBProductKey
                                Where Prod.PBProductKey = p.PBProductKey
                                AND Part.PBProductParticipationKey = pp.PBProductParticipationKey) 
            AND M.EnrolleeTypeKey = '1044'
            AND M.PLPlanKey = #{@pl_plan_key}
    SQL
  end
end