def sql_to_make_hex_grid(
        extent_table: str,
        epsg: int,
        output_table_name: str,
        size: float
):
    """
    This function returns a string with a valid SQL query that creates
    a hexagon layer covering the input {extent_table}.

    :param extent_table: 'name_of_table_to_cover'
    :param epsg: integer for EPSG you want the hexagons to be in
    :param output_table_name: 'hexagon_layer'
    :param size: float value, 1 = 1 square KM
    :return: string, valid SQL query
    """

    create_hex_grid_command = f"""
    DROP TABLE IF EXISTS {output_table_name};
    CREATE TABLE {output_table_name} (
      gid SERIAL NOT NULL PRIMARY KEY,
      geom GEOMETRY('POLYGON', {epsg}, 2) NOT NULL
    )
    WITH (OIDS=FALSE);  

    INSERT INTO {output_table_name} (geom)
    SELECT hex_grid({size},
                    (select st_xmin(st_transform(st_collect(geom), 4326)) from {extent_table}),
                    (select st_ymin(st_transform(st_collect(geom), 4326)) from {extent_table}),
                    (select st_xmax(st_transform(st_collect(geom), 4326)) from {extent_table}),
                    (select st_ymax(st_transform(st_collect(geom), 4326)) from {extent_table}),
                    4326, {epsg}, {epsg});"""

    return create_hex_grid_command.replace('\n', '')