import itertools


def create_segfrac(connector, segfrac, seasons, hours):
    """
    Generates the "SegFrac" table for the Temoa database.
    This table defines what fraction of a year is represented
    by each time slice.

    Parameters
    ----------
    connector : sqlite3 connection object
        Used to connect to and write to an sqlite database.
    segfrac : float
        The fraction-of-a-year represented by each time slice.
    seasons : list
        The list of seasons in the simulation.
    hours : list
        The list of hours in the simulation.

    Returns
    -------
    table_command : string
        The command for generating the "SegFrac" table.
    """

    table_command = """CREATE TABLE "SegFrac" (
                    	"season_name"	text,
                    	"time_of_day_name"	text,
                    	"segfrac"	real CHECK("segfrac" >= 0 AND "segfrac" <= 1),
                    	"segfrac_notes"	text,
                    	PRIMARY KEY("season_name","time_of_day_name"),
                    	FOREIGN KEY("season_name") REFERENCES "time_season"("t_season"),
                    	FOREIGN KEY("time_of_day_name") REFERENCES "time_of_day"("t_day")
                    );"""
    insert_command = """
                     INSERT INTO "SegFrac" VALUES (?,?,?,?)
                     """
    time_slices = itertools.product(seasons, hours)
    entries = [(ts[0][0], ts[1][0], segfrac, 'fraction of year')
               for ts in time_slices]

    cursor = connector.cursor()
    cursor.execute(table_command)
    cursor.executemany(insert_command, entries)
    connector.commit()

    return table_command