import sys

class ControllerRecord:
    def __init__(self, controller_id, location, ip, control_type, name, serial, mac, version, model, security, disconnected_time, calendar_id, access_level, connected_time):
        self.controller_id = controller_id
        self.location = location
        self.ip = ip
        self.control_type = control_type
        self.name = name
        self.serial = serial
        self.mac = mac
        self.version = version
        self.model = model
        self.security = security
        self.disconnected_time = disconnected_time
        self.calendar_id = calendar_id
        self.access_level = access_level
        self.connected_time = connected_time


f = open(sys.argv[1], mode="r")

dump_file = open(sys.argv[2], mode="w")

rows = f.read().split("\n")

dump_file.write("""
CREATE TABLE evi.controller
(
    controller_id text COLLATE pg_catalog."default" NOT NULL,
    location text COLLATE pg_catalog."default",
    calendar_id text COLLATE pg_catalog."default",
    access_level integer DEFAULT 0,
    ip text COLLATE pg_catalog."default" NOT NULL,
    control_type text COLLATE pg_catalog."default",
    name text COLLATE pg_catalog."default" NOT NULL,
    serial text COLLATE pg_catalog."default" NOT NULL,
    mac text COLLATE pg_catalog."default" NOT NULL,
    version text COLLATE pg_catalog."default",
    model text COLLATE pg_catalog."default" NOT NULL,
    security boolean NOT NULL,
    pass_through boolean NOT NULL DEFAULT false,
    disconnected_time timestamp with time zone NOT NULL,
    connected_time timestamp with time zone DEFAULT to_timestamp((0)::double precision)
    );\n""")

for row in rows:
    if len(row) == 0:
        continue

    cols = row.split(",")
    record = ControllerRecord(cols[0], cols[1], cols[2], cols[3], cols[4], cols[5], cols[6], cols[7], cols[8], cols[9], cols[10], cols[11], cols[12], cols[13])

    calendar_id_str = f"'{record.calendar_id}'" if record.calendar_id else "NULL"
    pass_through = "false"
    dump_file.write(f"""INSERT INTO evi.controller (controller_id, location, calendar_id, access_level, ip,
                     control_type, name, serial, mac, version, model, security, pass_through, disconnected_time, connected_time)
                        VALUES ('{record.controller_id}', '{record.location}', {calendar_id_str}, '{record.access_level}', '{record.ip}',
                                '{record.control_type}', '{record.name}', '{record.serial}', '{record.mac}', '{record.version}', '{record.model}',
                                '{record.security}', '{pass_through}', '{record.disconnected_time}', '{record.connected_time}');\n""")