import os, base64, json
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders

# Load token
token_path = os.path.expanduser('~/AppData/Local/hermes/google_token.json')
with open(token_path) as f:
    token_data = json.load(f)
creds = Credentials.from_authorized_user_info(token_data)
service = build('gmail', 'v1', credentials=creds)

# Tao email
msg = MIMEMultipart()
msg['To'] = 'ngoducminh84@gmail.com'
msg['From'] = 'ngoducminh84@gmail.com'
msg['Subject'] = 'Tai lieu ERP - Yeu cau chuan bi truoc Kick-off & Cau hoi lam viec FPT'

# Noi dung
body = """
Xin chao anh,

Em gui anh cac tai lieu da chuan bi cho buoi hop voi FPT hom nay (09/07/2026):

1. YEU_CAU_CHUAN_BI_TRUOC_KICKOFF.md
   - Tong hop day du cac yeu cau tu Microsoft & FPT:
   + Danh sach nhan su du an (BGĐ, PMO, Key Users)
   + Tai lieu quy trinh & thong ke hoat dong (Sizing)
   + Bo cau hoi khao sat nghiep vu (306 cau/14 phan he)
   + Tai lieu nghiep vu hien co can gom lai
   + Cong viec ky thuat & License D365
   + Checklist 9 viec kem deadline

2. CAU_HOI_LAM_VIEC_FPT.md
   - 7 nhom cau hoi de anh trao doi voi FPT:
   + Pham vi & muc tieu du an
   + To chuc nhan su
   + Phuong phap & lo trinh
   + Lich khao sat tung buoi
   + Tai lieu & du lieu
   + Hop dong & chi phi
   + Rui ro can luu y
   - Kem checklist 6 viec cho buoi hop hom nay

Tat ca file nam trong thu muc: ERP/Kick-off/

Anh xem va gui cho FPT nhe.
"""

msg.attach(MIMEText(body, 'plain', 'utf-8'))

# Dinh kem file 1
files = [
    (r'C:\antigra-DS\CLAW_LAM_VIEC\ERP\Kick-off\YEU_CAU_CHUAN_BI_TRUOC_KICKOFF.md', 'YEU_CAU_CHUAN_BI_TRUOC_KICKOFF.md'),
    (r'C:\antigra-DS\CLAW_LAM_VIEC\ERP\Kick-off\CAU_HOI_LAM_VIEC_FPT.md', 'CAU_HOI_LAM_VIEC_FPT.md'),
    (r'C:\antigra-DS\CLAW_LAM_VIEC\ERP\Kick-off\LICH_KHAO_SAT_ERP.md', 'LICH_KHAO_SAT_ERP.md'),
]

for file_path, filename in files:
    if os.path.exists(file_path):
        with open(file_path, 'rb') as f:
            attachment = MIMEBase('application', 'octet-stream')
            attachment.set_payload(f.read())
            encoders.encode_base64(attachment)
            attachment.add_header('Content-Disposition', 'attachment', filename=filename)
            msg.attach(attachment)
            print(f'Dinh kem: {filename}')
    else:
        print(f'Khong tim thay: {file_path}')

# Gui
raw = base64.urlsafe_b64encode(msg.as_bytes()).decode()
result = service.users().messages().send(userId='me', body={'raw': raw}).execute()
print(f'\nDa gui email! Message ID: {result["id"]}')
