import pandas as pd
import numpy as np
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
pd.set_option('display.max_colwidth', -1)

df = pd.read_csv('../../data/processed/complaints-before-state-updates.csv')

"""
Explanation: This is an analysis of complaints data, munged in ../notebooks/mung.ipynb.
Raw data is in ../data/raw
The fields are:
1. <i>abuse_number:</i> A unique number assigned each complaint.
2. <i>facility_id:</i> A unique number to each facility building. Stays if ownership changes.
3. <i>facility_name:</i> Name of facility as of January 2017, when DHS provided the facility data to The Oregonian.
4. <i>abuse_type:</i> A – facility abuse; L – licensing. Note: This does not apply to nursing facilities. All their complaints are either blank in this field or licensing.
5. <i>action_notes:</i> DHS determination of what general acts constituted the abuse or rule violation.
6. <i>incident_date:</i> Date the incident occured
7. <i>outcome:</i> A very brief description of the consequences of the abuse or rule violation to the reisdent
8. <i>outcome_notes:</i> A detailed description of what happened.
9. <i>year:</i> year incident occured
10. <i>fac_name:</i> If complaint is online, name listed for the facility
11. <i>public:</i> Whether or not complaint is online
End of explanation
"""


df.count()[0]

df[df['public']=='offline'].count()[0]

"""
Explanation: <h3>How many complaints do not appear in the state's public database?</h3>
End of explanation
"""


df[df['public']=='online'].count()[0]

"""
Explanation: <h3>How many complaints do appear in the state's public database?</h3>
End of explanation
"""


df[df['public']=='offline'].count()[0]/df.count()[0]*100

"""
Explanation: <h3>What percent of complaints are missing?</h3>
End of explanation
"""


df[(df['outcome']=='Exposed to Potential Harm') | (df['outcome']=='No Negative Outcome')].count()[0]

"""
Explanation: <h3>How many complaints were labelled 'Exposed to potential harm' or 'No negative outcome?'</h3>
End of explanation
"""


df[(df['outcome']=='Exposed to Potential Harm') |
   (df['outcome']=='No Negative Outcome')].count()[0]/df[df['public']=='offline'].count()[0]*100

"""
Explanation: <h3>Of all missing complaints, what percent are in the above two categories?</h3>
End of explanation
"""


df[(df['abuse_type']=='A') & (df['public']=='offline')].count()[0]

"""
Explanation: <h3>How many complaints are labelled 'A,' which stands for abuse, but are offline?</h3>
End of explanation
"""


totals = df.groupby(['omg_outcome','public']).count()['abuse_number'].unstack().reset_index()

totals.fillna(0, inplace = True)

totals['total'] = totals['online']+totals['offline']

totals['pct_offline'] = round(totals['offline']/totals['total']*100)

totals.sort_values('total',ascending=False)

df[df['abuse_number'].str.contains('ES147799')]

df_old[df_old['public']=='offline'].groupby('abuse_type').count()

df[(df['public']=='offline') & (df['abuse_type']=='A')]

"""
Explanation: <h3>What's the online/offline breakdown by outcome?</h3>
End of explanation
"""


