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


df = pd.read_csv('../../data/processed/complaints-3-29-scrape.csv')
owners = pd.read_csv('../../data/raw/APD_HistOwner.csv')

"""
Explanation: Data were munged here.
End of explanation
"""


owners.rename(columns={'HOW_IdNumber':'owner_id','HOW_CcmuNumber': 'fac_ccmunumber', 'HOW_DateActive':'license_date'}, inplace=True)
owners['license_date'] = pd.to_datetime(owners['license_date'])

owners = owners[['fac_ccmunumber','license_date','owner_id']]

"""
Explanation: <h5>First: Prep ownership history table</h5>
End of explanation
"""


#Last ownership change
owners[owners['fac_ccmunumber']=='50M132']

"""
Explanation: <h3>When did River Grove open, when did the last owners take over, and how many companies have owned the facility?</h3>
End of explanation
"""


#Slice of public River Grove complaints
rg = df[(df['facility_id']=='50M132') & (df['public']=='online')]

rg[rg['incident_date']>='2015-04-01'].count()[0]

"""
Explanation: <h3>How many visible complaints have there been under the current ownership?</h3>
End of explanation
"""


rg[rg['incident_date']<'2015-04-01'].count()[0]

"""
Explanation: <h3>How many online complaints have there been under previous ownership?</h3>
End of explanation
"""


rg[(rg['incident_date']<'2015-04-01') & (rg['incident_date']>'2013-04-01')].count()[0]

"""
Explanation: <h3>How many complaints occurred in the two years before the current owners took over?</h3>
End of explanation
"""


rg[['incident_date','online_fac_name']].drop_duplicates(subset='online_fac_name', keep='first').sort_values('incident_date', ascending=False)

"""
Explanation: <h3>What are the names River Grove is listed under on the public-facing website, and in what historical order?</h3>
End of explanation
"""


