from __future__ import print_function # Python 2/3 compatibility
import boto3
import json
from decimal import Decimal
from faker import Faker 
import time
from datetime import datetime
import faker_commerce
from random import randrange
import argparse
import copy
import os
import numpy as np
import logging

from botocore.exceptions import ClientError

fake = Faker() 
fake.add_provider(faker_commerce.Provider)

#REMOVE []; KEEP ''
dynamodb = boto3.resource('dynamodb',aws_access_key_id=['ACCESS_KEY'], aws_secret_access_key=['SECRET_KEY'])


table = dynamodb.Table(['YOUR_DDB_TABLE']) 

#to generate random data
customers = 100000 #load customer records into dynamodb

def generate_data():

    for c in range(0,customers):
        
        itemlist = []
    
        customerid = fake.user_name()
        
        for o in range(0,randrange(10)):
        
            orderid =  fake.uuid4()
            
            numberofitems = np.random.randint(1,4)
        
            itemlist.append(
                {
                    'pk' : 'CUST#'+customerid,
                    'sk' : 'ORD#'+orderid,
                    'type': 'order',
                    'orderid': orderid,
                    'orderdate' : fake.date_time_between(start_date='-2y', end_date='now').isoformat(),
                    'amount': np.random.randint(10,1000),
                    'numberofitems': numberofitems,
                    'GSI1pk': 'ORD#'+orderid,
                    'GSI1sk': 'ORD#'+orderid
                    }
                )
            
            for p in range(0,numberofitems):
                
                productid = fake.uuid4()
                
                itemlist.append(
                    {
                        'pk': 'ORD#'+orderid+'#PROD#'+productid,
                        'sk': 'ORD#'+orderid+'#PROD#'+productid,
                        'type': 'product',
                        'orderid': orderid,
                        'productid': productid,
                        'productname' : fake.ecommerce_name(),
                        'color' : fake.color(),
                        'category' : fake.ecommerce_category(),
                        'price' : np.random.randint(1,100),
                        'GSI1pk': 'ORD#'+orderid,
                        'GSI1sk': 'PROD#'+productid
                        }
                    )
                    
    
        itemlist.append(    {
                'pk': 'CUST#'+customerid,
                'sk': 'CUST#'+customerid,
                'type': 'customer',
                'username': customerid,
                'email': fake.email(),
                'fullname': fake.name(),
                'address':
                    {
                        'buildingnumber': fake.building_number(),
                        'streetaddress': fake.street_name(),
                        'postcode': fake.postcode(),
                        'city': fake.city(),
                        'country': fake.country_code()                        
                    }
                }
            )
        
        load_data(table,itemlist,c)
        
def load_data(table,itemlist,customers):
    
    try:
        with table.batch_writer() as batch:
    
            for index in range(0,len(itemlist)):
                
                batch.put_item(
                    Item=itemlist[index]
                    )
        
        if customers > 0 and customers % 1000 == 0:
                    print("loaded " + str(customers) + " customer records...")
    
    except Exception as e:
        print(f"Something went wrong with the load! Here's the error: {e}")
        
    
if __name__ == "__main__":
    
    print("loading dynamodb table..")
    generate_data()
