class GetIdCardDataTpaQuery
  def initialize(family_id)
    @family_id = ActiveRecord::Base.connection.quote(family_id)
    if family_id.length == 9
      @entity_type = ActiveRecord::Base.connection.quote("FamilyId")
    else
      @entity_type = ActiveRecord::Base.connection.quote("PBEntityKey")
    end
  end

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

  def to_id_card_data
    raw_data = call

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

    [id_card_data]
  end

  private

  def sql
    <<~SQL
      SELECT DISTINCT ROW_NUMBER() OVER(ORDER BY pbm.FamilyId ASC) as Id, pbc.MBMemberKey, 
                isnull(ou.CompanyNumber,'') as MbrType, 
                  CASE WHEN m.PLPlanKey = '14' THEN 'DUPLIN COUNTY GOVERNMENT' ELSE substring(div.LastName,0,41) END 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 
          Inner 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 PBProductKey From PBProduct Where FullDescription NOT LIKE '%LIFE%' AND FullDescription NOT LIKE '%VISION%' AND FullDescription NOT LIKE '%DENT%')  
            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 pbm.#{@entity_type}=#{@family_id}
            AND M.EnrolleeTypeKey='1044'
    SQL
  end
end
