import csv
import os, sys
import io
from optparse import OptionParser
import psutil
import uuid
import datetime, time, arrow
import random
import string
from mako.template import Template
import collections
import decimal
import gzip
import zipfile
import itertools
import traceback
import multiprocessing
import logging
import json
import mako
import redis
import sqlalchemy
from sqlalchemy.sql import expression as sa_exp
from sqlalchemy.schema import CreateTable
import sqlparse




if __name__ == '__main__':

	end_date_arw = arrow.utcnow()
	start_date_arw = end_date_arw.shift(days=-7)
	start_date_str = start_date_arw.format('YYYY-MM-DDTHH:MM:SS')+'Z'
	end_date_str = end_date_arw.format('YYYY-MM-DDTHH:MM:SS')+'Z'

	# query = 'SELECT LastName, Street, City, State, PostalCode, Phone, VendorLeadID__c, LeadSource, Status' \
	#         ' FROM Lead WHERE (Status != \'New\' AND Status != \'Bad Contact Info\') ' \
	#         'AND LastModifiedDate > ' + str(start_date.strftime("%Y-%m-%dT%H:%M:%SZ")) +\
	#         'AND LastModifiedDate < ' + str(end_date.strftime("%Y-%m-%dT%H:%M:%SZ"))
	# print(f'. query {query}')

	query = f''' 

		SELECT 

			ADDED_TO_CAMPAIGN__C, ADDRESS, ASSIGNED_USER_BRANCH__C, BALANCEBUCKET__C, BYTEFILENUM__C, 
			CD_DATETIME__C, CD_DATE__C, CALLLOOKUP__C, CALL_COUNTS_UPDATED__C, CALL_NOTES__C, CALLBACK_NEEDED__C, 
			CALLS_INBOUND__C, CALLS_OUTBOUND__C, CAMPAIGN__C, CITY, COMPANY, CONVEMAIL__C, CONVMOBILE__C, CONVPHONE__C, 
			CONVERTEDACCOUNTID, CONVERTEDCONTACTID, CONVERTEDDATE, CONVERTEDOPPORTUNITYID, COUNTRY, 
			CREATEDBYID, CREATEDDATE, CREDITPROFILE__C, CREDITPULLED__C, CREDITSCORE__C, CREDIT_DATE__C, 
			CREDIT_PULL_COUNT__C, CREDIT_PULL_TYPE__C, CURRLOANTYPE__C, CURRENTLOANINFOSTORED__C, CURRENT_RATE_TYPE__C, 
			CURRENT_USER_OWNS_RECORD__C, DAYS_SINCE_MODIFIED__C, DESIREDLA__C, DIRECT_MAIL_OPT_OUT__C, DONOTCALL, 
			ENC_CLTV__C, ENC_CURRENT_INTEREST_RATE__C, ENC_DEBT_PYMT_SAVINGS__C, ENC_ESTIMATED_VALUE__C, ENC_EXISTING_MTG_PAYOFF__C, 
			ENC_GFE_APPLICATION_DATE__C, ENC_INTEREST_RATE__C, ENC_LTV__C, ENC_LOAN_PROGRAM__C, ENC_LOAN_PURPOSE__C, ENC_LOAN_TYPE__C, 
			ENC_LOCK_EXPIRATION_DATE__C, ENC_LOCKED__C, ENC_MTG_PYMT_SAVINGS__C, ENC_OCCUPANCY__C, ENC_OTHER_DEBT_PAYOFFS__C, 
			ENC_PRESENT_EXP_HOA__C, ENC_PRESENT_EXP_HOI__C, ENC_PRESENT_EXP_MI__C, ENC_PRESENT_EXP_MTG_PYMT__C, 
			ENC_PRESENT_EXP_OTHER__C, ENC_PRESENT_EXP_RENT__C, ENC_PRESENT_EXP_SUBO__C, ENC_PRESENT_EXP_SUPP_HOI__C, 
			ENC_PRESENT_EXP_TAXES__C, ENC_PRESENT_EXP_TOTAL__C, ENC_PROPERTY_TYPE__C, ENC_PROPOSED_EXP_HOA__C, ENC_PROPOSED_EXP_HOI__C, 
			ENC_PROPOSED_EXP_MI__C, ENC_PROPOSED_EXP_MTG__C, ENC_PROPOSED_EXP_OTHER__C, ENC_PROPOSED_EXP_SUBO__C, 
			ENC_PROPOSED_EXP_SUPP_HOI__C, ENC_PROPOSED_EXP_TAXES__C, ENC_PROPOSED_EXP_TOTAL__C, ENC_QUAL_RATIO_BOTTOM__C, 
			ENC_QUAL_RATIO_TOP__C, ENC_TOTAL_LOAN_AMOUNT__C, EMAIL, EMAILBOUNCEDDATE, EMAILBOUNCEDREASON, ESTVALUE__C, 
			FIRSTNAME, FIVE9_DNIS__C, FLOW_TEST_RECORD__C, FULLID__C, GVG_RECYCLED__C, GEOCODEACCURACY, ID, INDIVIDUALID, 
			INDUSTRY, INTEREST_RATE__C, ISCONVERTED, ISDELETED, ISTEST__C, ISUNREADBYOWNER, ISVET__C, JB_CSR_LR_FIRST_CONTACT__C, 
			JIGSAW, JIGSAWCONTACTID, LF_COMPLETED__C, LM_DATE__C, LTVBUCKET__C, LASTACTIVITYDATE, LASTMODIFIEDBYID, LASTMODIFIEDDATE, 
			LASTNAME, LASTREFERENCEDDATE, LASTVIEWEDDATE, LAST_DNC_SCRUB__C, LATITUDE, LEADOWNERPHONE__C, LEADSOURCE, LEAD_OWNER_NAME__C, 
			LOANDATEBUCKET__C, LOAN_BALANCE__C, LONGITUDE, MASTERRECORDID, MIDDLENAME, MINI_APP_TYPE__C, MOBILEPHONE, NAME, NEEDS_RD__C, 
			NEW_LOAN_AMOUNT__C, NEW_LOAN_TYPE__C, NO_EMAIL_ADDRESS__C, NOTES__C, NUMBEROFEMPLOYEES, OWNERID, OWNER_ORG__C, PHONE, PHOTOURL, 
			POSTALCODE, PROPTYPE__C, QUALIFIED_LEAD__C, QUALIFYING_FICO__C, RATEBUCKET__C, RATING, REASSIGNMENT_STATUS__C, 
			RECYCLED_COUNT__C, RECYCLED__C, REFINANCE_TYPE__C, REFUND_NOTES__C, REFUND_REASON__C, REFUND_STATUS__C, REMOVE_FROM_WF__C, 
			REVIEW_LINK_SEND_FLAG__C, SMS_STATUS__C, SMS_TEST_RECORD__C, SMS_WORKING_DRIP_STARTED__C, SALUTATION, SEQUENCE__C,
			SOURCESITE__C, SPANISHSPEAKER__C, STATE, STATUS, STREET, SUB_STATUS__C, SUFFIX, SYSTEMMODSTAMP, TF_CERT_URL__C, 
			TITLE, UNQUAL_REASON__C, VENDORLEADID__C, VETERAN__C, WF_BYPASS__C, WEBSITE, SMAGICINTERACT__SMSOPTOUT__C

		WHERE 

			CREATEDDATE >= {start_date_str} AND CREATEDATE < {end_date_str}


	'''.lstrip().strip().replace('\t', '').replace('\n', '')


	print(query)








