import sys

class Camera:
    def __init__(
        self,
        camera_id,
        name,
        display_label,
        location,
        analytics,
        tcp,
        enabled,
        sensitivity,
        gap_time,
        grid,
        grid_x,
        grid_y,
        mode,
        archive_days,
        pre_buffer,
        login,
        password,
        port,
        max_object_count,
        active_md,
        registr,
        mode_md,
        md_profile,
        size_x,
        size_y,
    ):
        self.camera_id = camera_id
        self.name = name
        self.display_label = display_label
        self.location = location
        self.analytics = analytics
        self.tcp = tcp
        self.enabled = enabled
        self.sensitivity = sensitivity
        self.gap_time = gap_time
        self.grid = grid
        self.grid_x = grid_x
        self.grid_y = grid_y
        self.mode = mode
        self.archive_days = archive_days
        self.pre_buffer = pre_buffer
        self.login = login
        self.password = password
        self.port = port
        self.max_object_count = max_object_count
        self.active_md = active_md
        self.registr = registr
        self.mode_md = mode_md
        self.md_profile = md_profile
        self.size_x = size_x
        self.size_y = size_y

def main():
    if len(sys.argv) < 3:
        print("Usage: python script.py <input_csv> <output_sql>")
        return 1

    try:
        f = open(sys.argv[1], mode="r", encoding="utf-8")
        dump_file = open(sys.argv[2], mode="w", encoding="utf-8")
    except Exception as e:
        print("Ошибка открытия файлов:", e)
        return 1

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

    dump_file.write(
        """
    CREATE TABLE evi.camera
    (
        camera_id text COLLATE pg_catalog."default" NOT NULL,
        name text COLLATE pg_catalog."default" NOT NULL,
        display_label boolean NOT NULL,
        location text COLLATE pg_catalog."default",
        analytics boolean NOT NULL,
        enabled boolean NOT NULL,
        sensitivity real NOT NULL,
        gap_time integer NOT NULL,
        grid text COLLATE pg_catalog."default" NOT NULL,
        grid_x integer NOT NULL,
        grid_y integer NOT NULL,
        mode integer NOT NULL,
        archive_days integer NOT NULL,
        pre_buffer integer NOT NULL DEFAULT 5,
        login text COLLATE pg_catalog."default",
        password text COLLATE pg_catalog."default",
        port text COLLATE pg_catalog."default",
        max_object_count integer NOT NULL DEFAULT 20,
        active_md boolean NOT NULL DEFAULT false,
        registr boolean NOT NULL DEFAULT true,
        mode_md boolean NOT NULL DEFAULT false,
        md_profile text COLLATE pg_catalog."default",
        size_x real,
        size_y real,
        CONSTRAINT camera_pkey PRIMARY KEY (camera_id),
        CONSTRAINT camera_location_fkey FOREIGN KEY (location)
            REFERENCES evi.location (location_id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE SET NULL,
        CONSTRAINT camera_md_profile_fkey FOREIGN KEY (md_profile)
            REFERENCES evi.md_profile (profile_name) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE SET NULL
    )

    TABLESPACE pg_default;

    ALTER TABLE IF EXISTS evi.camera
        OWNER to core;
    \n"""
    )

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

        cols = row.split("|")
        record = Camera(
            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],
            cols[14],
            cols[15],
            cols[16],
            cols[17],
            cols[18],
            cols[19],
            cols[20],
            cols[21],
            cols[22],
            cols[23],
            cols[24],
        )

        record.md_profile = (
            "'" + record.md_profile + "'" if record.md_profile else "NULL"
        )
        record.size_x = record.size_x or "NULL"
        record.size_y = record.size_y or "NULL"

        dump_file.write(
            f"""INSERT INTO evi.camera (camera_id, name, display_label, location, analytics, enabled, sensitivity, gap_time, grid, grid_x,
        grid_y, mode, archive_days, pre_buffer, login, password, port, max_object_count, active_md, registr, mode_md, md_profile, size_x, size_y)
                        VALUES ('{record.camera_id}', '{record.name}', '{record.display_label}', '{record.location}', '{record.analytics}',
                        '{record.enabled}', '{record.sensitivity}', '{record.gap_time}', '{record.grid}', '{record.grid_x}', '{record.grid_y}',
                        '{record.mode}', '{record.archive_days}', '{record.pre_buffer}', '{record.login}', '{record.password}', '{record.port}',
                        '{record.max_object_count}', '{record.active_md}', '{record.registr}', '{record.mode_md}', {record.md_profile}, {record.size_x}, {record.size_y});\n"""
        )


if __name__ == "__main__":
    main()
