import pandas as pd


from great_tables import GT, loc, style


COLUMNS = ["topic", "group", "carter", "reagan", "anderson", "carter_ford"]


def load_data():
    return [
        ("Party", "Democrats (43%)", 66, 26, 6, "77 to 22"),
        ("Party", "Independents (23%)", 30, 54, 12, "43 to 54"),
        ("Party", "Republicans (28%)", 11, 84, 4, "9 to 90"),
        ("Ideology", "Liberals (17%)", 57, 27, 11, "70 to 26"),
        ("Ideology", "Moderates (46%)", 42, 48, 8, "51 to 48"),
        ("Ideology", "Conservatives (28%)", 23, 71, 4, "29 to 70"),
    ]


def value_table(data):
    return {
        "columns": [
            "Topic",
            "Voter group",
            "Carter 1980 (%)",
            "Reagan 1980 (%)",
            "Anderson 1980 (%)",
            "Carter and Ford 1976 (%)",
        ],
        "rows": [list(row) for row in data],
    }


def warm_editorial_table(table: GT) -> GT:
    return (
        table.opt_table_font(font=["Inter", "system-ui", "sans-serif"])
        .opt_align_table_header(align="left")
        .tab_options(
            table_background_color="#FFF7ED",
            table_font_color="#1C1917",
            table_font_size="14px",
            table_border_top_style="none",
            table_border_bottom_color="#E7DDD2",
            heading_title_font_size="18px",
            heading_subtitle_font_size="13px",
            heading_border_bottom_style="none",
            column_labels_font_weight="600",
            column_labels_border_top_style="none",
            column_labels_border_bottom_color="#1C1917",
            column_labels_border_bottom_width="1px",
            column_labels_vlines_style="none",
            table_body_hlines_color="#E7DDD2",
            table_body_vlines_style="none",
            table_body_border_bottom_color="#E7DDD2",
            row_group_font_weight="600",
            row_group_border_top_color="#E7DDD2",
            row_group_border_bottom_color="#E7DDD2",
            stub_border_style="none",
            summary_row_border_color="#1C1917",
            grand_summary_row_border_color="#1C1917",
            source_notes_font_size="12px",
        )
        .tab_style(
            style=style.text(font="Georgia, 'Times New Roman', serif", weight="600"),
            locations=loc.title(),
        )
        .tab_style(style=style.text(color="#57534E"), locations=loc.subtitle())
        .tab_style(style=style.text(color="#57534E"), locations=loc.source_notes())
    )


def vote_table(data) -> GT:
    frame = pd.DataFrame(data, columns=COLUMNS)
    return warm_editorial_table(
        GT(frame, rowname_col="group", groupname_col="topic").cols_label(
            carter="Carter", reagan="Reagan", anderson="Anderson", carter_ford="Carter and Ford"
        )
    )


def make_before(data) -> GT:
    return vote_table(data)


if __name__ == "__main__":
    print(make_before(load_data()).as_raw_html())
