import argparse

oldIOCdata = []
updatedIOCdata = []
revertedIOCdata = []

# establishes correct file read paths
def commandoptions():
    parser = argparse.ArgumentParser(description= 'Establishes 3 files to read from; compares the data from all 3')
    parser.add_argument('--r1', required=True, help='Defines the first file to read from, expects screenioc file')
    parser.add_argument('--r2', required=True, help='Defines the second file to read from, expects revertedscreenioc file')
    parser.add_argument('--r3', required=True, help='Defines the third file to read from, expects updatedscreenioc file')
    
    args = parser.parse_args()

    oldscreenIOCs = args.r1
    RevertedscreenIOCs = args.r2
    newScreenIOCs = args.r3

    return oldscreenIOCs, RevertedscreenIOCs, newScreenIOCs

def compareIOCfiles(oldscreenIOCs, RevertedscreenIOCs, newScreenIOCs):
    with open(oldscreenIOCs) as f1, open(RevertedscreenIOCs) as f2, open(newScreenIOCs) as f3:
        for line in f1.read().splitlines():
            trimmed_line = line.strip()

            if not trimmed_line or trimmed_line.startswith('#'):
                continue

            columns = line.split(None, 4)
            oldIOCdata.append(columns[0])

        for line in f2.read().splitlines():
            trimmed_line = line.strip()

            if not trimmed_line or trimmed_line.startswith('#'):
                continue

            columns = line.split(None, 4)
            updatedIOCdata.append(columns[0])    
        
        for line in f3.read().splitlines():
            trimmed_line = line.strip()

            if not trimmed_line or trimmed_line.startswith('#'):
                continue

            columns = line.split(None, 4)
            revertedIOCdata.append(columns[1])

    for item in oldIOCdata:
        if item not in updatedIOCdata:
            raise ValueError("The following IOC was not found in updatedIOCdata: {}".format(item))
        else:
            if item not in revertedIOCdata:
                raise ValueError("The following IOC was not found in revertedIOCdata: {}".format(item))
            
    for item in updatedIOCdata:
        if item not in oldIOCdata:
            raise ValueError("The following IOC was not found in oldIOCdata: {}".format(item))
        else:
            if item not in revertedIOCdata:
                raise ValueError("The following IOC was not found in revertedIOCdata: {}".format(item))
            
    for item in revertedIOCdata:
        if item not in oldIOCdata:
            raise ValueError("The following IOC was not found in oldIOCdata: {}".format(item))
        else:
            if item not in updatedIOCdata:
                raise ValueError("The following IOC was not found in updatedIOCdata: {}".format(item))
        
    print('The data in the three documents are synonymous')

oldscreenIOCs, RevertedscreenIOCs, newScreenIOCs = commandoptions()

compareIOCfiles(oldscreenIOCs, RevertedscreenIOCs, newScreenIOCs)