from math import cos, pi, sin


import plotly.graph_objects as go


Row = tuple[str, str, int]


def value_table(data: list[Row]) -> dict[str, list]:
    return {"columns": ["Team A", "Team B", "Shared projects"], "rows": [list(row) for row in data]}


def _style_figure(fig: go.Figure, **layout: object) -> go.Figure:
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title={"text": "Shared projects between teams", "x": 0.02, "xanchor": "left"},
        margin={"l": 72, "r": 32, "t": 84, "b": 64},
        showlegend=False,
        **layout,
    )
    return fig


def load_data() -> list[Row]:
    return [
        ("Design", "Support", 2),
        ("Design", "Sales", 4),
        ("Design", "Finance", 0),
        ("Design", "Research", 6),
        ("Design", "Product", 5),
        ("Support", "Sales", 5),
        ("Support", "Finance", 1),
        ("Support", "Research", 2),
        ("Support", "Product", 3),
        ("Sales", "Finance", 3),
        ("Sales", "Research", 1),
        ("Sales", "Product", 4),
        ("Finance", "Research", 2),
        ("Finance", "Product", 1),
        ("Research", "Product", 5),
    ]


def _team_names(data: list[Row]) -> list[str]:
    return list(dict.fromkeys(team for left, right, _ in data for team in (left, right)))


def make_after(data: list[Row]) -> go.Figure:
    teams = sorted(_team_names(data))
    counts = {frozenset((left, right)): count for left, right, count in data}
    matrix = [[counts.get(frozenset((left, right))) for right in teams] for left in teams]
    fig = go.Figure(
        go.Heatmap(
            x=teams,
            y=teams,
            z=matrix,
            colorscale=[[0, "#f2f7fb"], [1, "#86aecb"]],
            zmin=0,
            zmax=6,
            xgap=2,
            ygap=2,
            text=[["" if value is None else str(value) for value in row] for row in matrix],
            texttemplate="%{text}",
            textfont={"color": "#172b3b"},
            showscale=False,
            hoverongaps=False,
            hovertemplate="%{y} and %{x}: %{z} shared projects<extra></extra>",
        )
    )
    return _style_figure(
        fig,
        xaxis={"tickangle": 0, "side": "top"},
        yaxis={"autorange": "reversed", "automargin": True},
        plot_bgcolor="#ffffff",
    )


if __name__ == "__main__":
    print(make_after(load_data()).to_json())
