from leximpact_survey_scenario.graphiques_cas_type.decomposition import (
    decomposition_data,
)
from leximpact_survey_scenario.test_case.cas_type_utils import tax_and_benefit_system

import plotly.graph_objects as go
import numpy as np
year = 2023
smic = (
    tax_and_benefit_system.parameters(
        year
    ).marche_travail.salaire_minimum.smic.smic_b_mensuel
    * 12
)
donnees_celib = decomposition_data(
    year=year,
    count=50,
    nb_smic_max=4,
    categorie_salarie="prive_non_cadre",
    parent1_age=40,
    couple=False,
    union_legale=False,
    biactif=False,
    parent2_age=None,
    loyer_mensuel=600,
    statut_occupation_logement="locataire",
    zone_apl=2,
)

donnees_uniactifs = decomposition_data(
    year=year,
    count=50,
    nb_smic_max=4,
    categorie_salarie="prive_non_cadre",
    parent1_age=40,
    couple=True,
    union_legale=True,
    biactif=False,
    parent2_age=38,
    # nb_enfants=0, enfants_age=range(0),
    loyer_mensuel=1000,
    statut_occupation_logement="locataire",
    zone_apl=2,
)

donnees_biactifs = decomposition_data(
    year=year,
    count=50,
    nb_smic_max=4,
    categorie_salarie="prive_non_cadre",
    parent1_age=40,
    couple=True,
    union_legale=True,
    biactif=True,
    parent2_age=38,
    loyer_mensuel=1000,
    statut_occupation_logement="locataire",
    zone_apl=2,
)

donnees_biactifs_enfant = decomposition_data(
    year=year,
    count=50,
    nb_smic_max=4,
    categorie_salarie="prive_non_cadre",
    parent1_age=40,
    couple=True,
    union_legale=True,
    biactif=True,
    parent2_age=38,
    nb_enfants=1,
    enfants_age=[10],
    loyer_mensuel=1600,
    statut_occupation_logement="locataire",
    zone_apl=2,
)

donnees_biactifs_enfants = decomposition_data(
    year=year,
    count=50,
    nb_smic_max=4,
    categorie_salarie="prive_non_cadre",
    parent1_age=40,
    couple=True,
    union_legale=True,
    biactif=True,
    parent2_age=38,
    nb_enfants=3,
    enfants_age=[6, 8, 10],
    loyer_mensuel=1600,
    statut_occupation_logement="locataire",
    zone_apl=2,
)
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: divide by zero encountered in divide
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: invalid value encountered in multiply
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: divide by zero encountered in divide
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: invalid value encountered in multiply
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: divide by zero encountered in divide
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: invalid value encountered in multiply
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: divide by zero encountered in divide
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: invalid value encountered in multiply
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: divide by zero encountered in divide
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: invalid value encountered in multiply
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: divide by zero encountered in divide
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: invalid value encountered in multiply
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: divide by zero encountered in divide
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: invalid value encountered in multiply
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: divide by zero encountered in divide
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: invalid value encountered in multiply
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: divide by zero encountered in divide
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: invalid value encountered in multiply
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: divide by zero encountered in divide
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
/home/cgl/leximpact/leximpact-survey-scenario/.venv/lib/python3.10/site-packages/openfisca_france/model/prelevements_obligatoires/prelevements_sociaux/cotisations_sociales/travail_non_salarie.py:199: RuntimeWarning: invalid value encountered in multiply
  - (0.007 * (assiette_pss > 5) * ((assiette_pss - 5) / assiette_pss))
datas = {
    "celibataire": donnees_celib,
    "couple un actif": donnees_uniactifs,
    "couple deux actifs": donnees_biactifs,
    "couple actifs 1 enfant": donnees_biactifs_enfant,
    "couple actifs 3 enfants": donnees_biactifs_enfants,
}
for data in [
    "celibataire",
    "couple un actif",
    "couple deux actifs",
    "couple actifs 1 enfant",
    "couple actifs 3 enfants",
]:
    graph_data = datas[data]
    graph_data["cumul"] = (
        graph_data["salaire_de_base"] + graph_data["prestations_sociales"]
    )
    graph_data["revenu_disponible2"] = np.where(
        graph_data["revenu_disponible"] < graph_data["cumul"],
        graph_data["revenu_disponible"],
        graph_data["cumul"],
    )
    graph_data = graph_data.loc[graph_data.salaire_de_base <= 4 * smic]
    fig = go.Figure(
        layout=go.Layout(
            title=data,
            template="plotly_white",
            legend=dict(
                orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1
            ),
            width=800,
            margin=dict(l=10, r=10, t=20, b=10),
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.prestations_sociales,
            stackgroup="one",
            name="Prestations sociales",
            line=dict(color="silver"),
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.salaire_de_base,
            mode="lines",
            stackgroup="one",
            name="Salaire brut",
            line=dict(color="powderblue"),
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.cumul,
            mode="lines",
            name="Salaire brut",
            showlegend=False,
            line=dict(color="gray"),
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.revenu_disponible2,
            fill="tonexty",
            fillpattern=dict(fillmode="replace", shape="/"),
            # fillpattern_shape= ['/'],
            name="Prelevements obligatoires",
            line=dict(color="gray"),
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.revenu_disponible,
            mode="lines",
            name="Revenu_disponible",
            line=dict(color="darkslateblue", width=4),
        )
    )
    fig.show()
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
for data in [
    "celibataire",
    "couple un actif",
    "couple deux actifs",
    "couple actifs 1 enfant",
    "couple actifs 3 enfants",
]:
    graph_data = datas[data]
    graph_data = graph_data.loc[graph_data.salaire_de_base <= 3 * smic]

    fig = go.Figure(layout=go.Layout(title=data, template="plotly_white"))
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.prestations_sociales,
            stackgroup="two",
            name="Prestations sociales",
        )
    )  #
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.salaire_de_base,
            stackgroup="two",
            name="Salaire brut",
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.impots_directs,
            stackgroup="three",
            name="IR",
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.cotisations_salariales,
            stackgroup="three",
            name="Cotisations salariales",
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.csg,
            stackgroup="three",
            name="CSG",
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.crds,
            stackgroup="three",
            name="CRDS",
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.revenu_disponible,
            name="Revenu disponible",
            line=dict(color="#0000ff", width=3),
        )
    )
    fig.show()
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
for data in [
    "celibataire",
    "couple un actif",
    "couple deux actifs",
    "couple actifs 1 enfant",
    "couple actifs 3 enfants",
]:
    graph_data = datas[data]
    graph_data = graph_data.loc[graph_data.salaire_de_base <= 4 * smic]

    fig = go.Figure(layout=go.Layout(title=data))
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.ir_ss_qf,
            stackgroup="one",
            name="Impot brut",
        )
    )  #'Impot brut'))
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.ir_plaf_qf,
            stackgroup="two",
            name="Impot après QF",
        )
    )  #'Avantage quotient_familial'))
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=-graph_data.irpp,
            stackgroup="three",
            name="Impot net",
        )
    )
    # fig.add_trace(go.Scatter(x = graph_data.salaire_de_base, y =graph_data.salaire_de_base, stackgroup='four', name='Salaire brut'))
    # fig.add_trace(go.Scatter(x = graph_data.salaire_de_base, y= graph_data.revenu_disponible, name = "Revenu disponible", line=dict(color="#0000ff", width = 3)))
    fig.show()
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
for data in [
    "celibataire",
    "couple un actif",
    "couple deux actifs",
    "couple actifs 1 enfant",
    "couple actifs 3 enfants",
]:
    graph_data = datas[data]
    graph_data = graph_data.loc[graph_data.salaire_de_base <= 3.6 * smic]

    fig = go.Figure(layout=go.Layout(title=data))
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.prestations_familiales,
            stackgroup="one",
            name="Prestations familiales",
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.minima_sociaux,
            stackgroup="one",
            name="Minima sociaux",
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.aides_logement,
            stackgroup="one",
            name="Aides au logement",
        )
    )
    # fig.add_trace(go.Scatter(x = graph_data.salaire_de_base, y =graph_data.salaire_de_base, stackgroup='one', name='Salaire brut'))
    # fig.add_trace(go.Scatter(x = graph_data.salaire_de_base, y= graph_data.revenu_disponible, name = "Revenu disponible", line=dict(color="#0000ff", width = 3)))
    fig.show()
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
for data in [
    "celibataire",
    "couple deux actifs",
    "couple actifs 1 enfant",
    "couple actifs 3 enfants",
]:  # "couple un actif",
    graph_data = datas[data]
    graph_data = graph_data.loc[graph_data.salaire_de_base <= 3.6 * smic]

    fig = go.Figure(layout=go.Layout(title=data, template="plotly_white"))
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base, y=graph_data.aah, stackgroup="one", name="AAH"
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.caah,
            stackgroup="one",
            name="CAAH",
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.minimum_vieillesse,
            stackgroup="one",
            name="Minimum vieillesse",
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base, y=graph_data.rsa, stackgroup="one", name="RSA"
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base, y=graph_data.ppa, stackgroup="one", name="PPA"
        )
    )
    # fig.add_trace(go.Scatter(x = graph_data.salaire_de_base, y =graph_data.salaire_de_base, stackgroup='one', name='Salaire brut'))
    # fig.add_trace(go.Scatter(x = graph_data.salaire_de_base, y= graph_data.revenu_disponible, name = "Revenu disponible", line=dict(color="#0000ff", width = 3)))
    fig.show()
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json
for data in ["couple actifs 1 enfant", "couple actifs 3 enfants"]:
    graph_data = datas[data]
    graph_data = graph_data.loc[graph_data.salaire_de_base <= 3.6 * smic]

    fig = go.Figure(layout=go.Layout(title=data, template="plotly_white"))
    # fig.add_trace(go.Scatter(x = graph_data.salaire_de_base, y =graph_data.prestations_familiales, stackgroup='two', name='total'))
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base, y=graph_data.af, stackgroup="one", name="af"
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base, y=graph_data.cf, stackgroup="one", name="cf"
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base, y=graph_data.ars, stackgroup="one", name="ars"
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base, y=graph_data.aes, stackgroup="one", name="aes"
        )
    )
    fig.add_trace(
        go.Scatter(
            x=graph_data.salaire_de_base,
            y=graph_data.paje,
            stackgroup="one",
            name="paje",
        )
    )
    # fig.add_trace(go.Scatter(x = graph_data.salaire_de_base, y =graph_data.crds_pfam, stackgroup='one', name='crds_pfam'))
    # fig.add_trace(go.Scatter(x = graph_data.salaire_de_base, y =graph_data.salaire_de_base, stackgroup='one', name='Salaire brut'))
    # fig.add_trace(go.Scatter(x = graph_data.salaire_de_base, y= graph_data.revenu_disponible, name = "Revenu disponible", line=dict(color="#0000ff", width = 3)))
    fig.show()
Unable to display output for mime type(s): application/vnd.plotly.v1+json
Unable to display output for mime type(s): application/vnd.plotly.v1+json

TODO :