from fastapi import APIRouter, Form, Depends, HTTPException, File, UploadFile,Request
from fastapi.responses import FileResponse
from fastapi_login.exceptions import InvalidCredentialsException
from fastapi_login import LoginManager
from fastapi.responses import HTMLResponse

import re
import pandas as pd
import ast
import pandas as pd
import csv
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc('font', family='Microsoft YaHei')
pd.set_option('display.max_columns', None)

data = APIRouter()

def fix_dict_format(s):
    if isinstance(s, str):
        pairs = re.findall(r"'([^']+)':(\d+)", s)
        fixed_dict = "{" + ", ".join([f"'{key}': {value}" for key, value in pairs]) + "}"
        return fixed_dict
    else:
        return s


def age_group(file):
    
    # 讀檔，把 position 轉成 dictionary 格式
    user_information = pd.read_csv(file)
    user_information['position'] = user_information['position'].apply(fix_dict_format)
    user_information['position'].fillna('{}', inplace=True)
    user_information['position'] = user_information['position'].apply(lambda x: ast.literal_eval(x) if pd.notnull(x) else {})
    # only學員
    user_information = user_information[user_information['position'].apply(lambda x: x.get('學員', 0)==1)]

    #user_information[user_information['gender'].str.strip() != '']['gender']
    
    # 生日
    birthday = pd.to_datetime(user_information['birthday'], format='%Y-%m-%d')

    # 年齡
    current_date = pd.to_datetime('today')
    user_information['age'] = ((current_date - birthday).dt.days / 365).round(1).astype(int)

    # 按年齡分群
    bins = [0, 18, 25, 35, 45, 55, 65, float('inf')]
    labels = ['18-', '18-24', '25-34', '35-44', '45-54', '55-64', '65+']
    user_information['age_group'] = pd.cut(user_information['age'], bins=bins, labels=labels, right=False)
    age_group_counts = user_information['age_group'].value_counts(sort=False)
    
    # 畫圖
    fig, ax = plt.subplots(figsize=(5, 3))
    age_group_counts.plot(kind='bar', rot=0, ax=ax)
    ax.set_title('Age Distribution')
    ax.set_xlabel('Age Group')
    ax.set_ylabel('Count')
    # plt.show()
    print(age_group_counts)
    return age_group_counts

@data.post("/print")
async def upload_file():
    
    age_group_counts = age_group('/var/www/ntcri_api/app/api/user_information_change.csv')    

    return {"data": age_group_counts.to_string()}
    

if __name__ == '__main__':
    age_group_counts = age_group('user_information_change.csv')
    print(age_group_counts)
