from pathlib import Path

import matplotlib.pyplot as plt
import pandas as pd

csv_path = Path(__file__).resolve().parents[2] / "retail_raw_reduced.csv"
dataset = pd.read_csv(csv_path)

dataset['order_date'] = pd.to_datetime(dataset['order_date'])
dataset['order_month'] = dataset['order_date'].dt.strftime('%Y-%m')

top_brands = (dataset[dataset['order_month'] == '2019-12'].groupby('brand')['quantity']
              .sum()
              .reset_index()
              .sort_values(by='quantity', ascending=False)
              .head(5))

dataset_top5brand_dec = dataset[(dataset['order_month'] == '2019-12') &
                                (dataset['brand'].isin(top_brands['brand'].to_list()))]

# membuat dataframe baru, untuk agregat jumlah quantity terjual per product
dataset_top5brand_dec_per_product = dataset_top5brand_dec.groupby(['brand','product_id'])['quantity'].sum().reset_index()

# beri kolom baru untuk menandai product yang terjual >= 100 dan <100
dataset_top5brand_dec_per_product['quantity_group'] = \
dataset_top5brand_dec_per_product['quantity'].apply(lambda x: '>= 100' if x >= 100 else '< 100')
dataset_top5brand_dec_per_product.sort_values('quantity', ascending=False, inplace=True)

# membuat referensi pengurutan brand berdasarkan banyaknya product
s_sort = dataset_top5brand_dec_per_product.groupby(['brand'])['product_id'].nunique().sort_values(ascending=False)

# plot stacked barchart
dataset_top5brand_dec_per_product.groupby(['brand','quantity_group'])['product_id'].nunique().unstack().reindex(index=s_sort.index, level='brand').plot(kind='bar', stacked=True)
plt.title('Number of Sold Products per Brand, December 2019', loc='center', pad=30, fontsize=15, color='blue')
plt.xlabel('Brand', fontsize=15)
plt.ylabel('Number of Products', fontsize=15)
plt.ylim(ymin=0)
plt.xticks(rotation=0)
plt.show()
