#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
PPTX 生成脚本
使用 python-pptx 创建 PowerPoint 演示文稿
"""

from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RgbColor
from pptx.enum.text import PP_ALIGN, MSO_ANCHOR
from datetime import datetime

def create_presentation(title, slides_data, output_path):
    """创建 PPTX 演示文稿"""
    
    # 创建演示文稿
    prs = Presentation()
    
    # 设置宽屏 16:9
    prs.slide_width = Inches(13.333)
    prs.slide_height = Inches(7.5)
    
    # 1. 标题页
    slide_layout = prs.slide_layouts[0]
    slide = prs.slides.add_slide(slide_layout)
    title_shape = slide.shapes.title
    subtitle_shape = slide.placeholders[1]
    
    title_shape.text = title
    subtitle_shape.text = f"生成日期：{datetime.now().strftime('%Y 年 %m 月 %d 日')}\n小诸 AI 助手 制作"
    
    # 2. 内容页
    for slide_data in slides_data:
        slide_layout = prs.slide_layouts[1]
        slide = prs.slides.add_slide(slide_layout)
        
        # 标题
        title_shape = slide.shapes.title
        title_shape.text = slide_data.get('title', '')
        
        # 内容
        if 'content' in slide_data:
            body_shape = slide.placeholders[1]
            tf = body_shape.text_frame
            tf.word_wrap = True
            
            for item in slide_data['content']:
                p = tf.add_paragraph()
                p.text = item.get('text', '')
                p.level = item.get('level', 0)
                p.font.size = Pt(item.get('font_size', 18))
                if item.get('bold', False):
                    p.font.bold = True
    
    # 3. 表格页
    for slide_data in slides_data:
        if slide_data.get('type') == 'table':
            slide_layout = prs.slide_layouts[5]  # 空白布局
            slide = prs.slides.add_slide(slide_layout)
            
            # 标题
            title_shape = slide.shapes.add_textbox(
                Inches(0.5), Inches(0.3), Inches(12), Inches(1)
            )
            title_shape.text_frame.text = slide_data.get('title', '')
            title_shape.text_frame.paragraphs[0].font.size = Pt(32)
            title_shape.text_frame.paragraphs[0].font.bold = True
            
            # 表格
            table_data = slide_data.get('table_data', [])
            if table_data:
                rows = len(table_data)
                cols = len(table_data[0]) if table_data else 0
                left = Inches(0.5)
                top = Inches(1.5)
                width = Inches(12)
                height = Inches(0.8)
                
                table = slide.shapes.add_table(rows, cols, left, top, width, height).table
                
                # 设置列宽
                for i in range(cols):
                    table.columns[i].width = width // cols
                
                # 填充数据
                for i, row in enumerate(table_data):
                    for j, cell in enumerate(row):
                        table.cell(i, j).text = str(cell)
                        if i == 0:  # 表头
                            table.cell(i, j).text_frame.paragraphs[0].font.bold = True
    
    # 4. 图表页 (使用文本框模拟)
    for slide_data in slides_data:
        if slide_data.get('type') == 'chart':
            slide_layout = prs.slide_layouts[5]
            slide = prs.slides.add_slide(slide_layout)
            
            # 标题
            title_shape = slide.shapes.add_textbox(
                Inches(0.5), Inches(0.3), Inches(12), Inches(1)
            )
            title_shape.text_frame.text = slide_data.get('title', '')
            title_shape.text_frame.paragraphs[0].font.size = Pt(32)
            
            # 图表内容
            if 'chart_data' in slide_data:
                left = Inches(1)
                top = Inches(1.5)
                for item in slide_data['chart_data']:
                    tb = slide.shapes.add_textbox(left, top, Inches(10), Inches(0.5))
                    tf = tb.text_frame
                    tf.text = item.get('label', '') + ': ' + item.get('value', '')
                    tf.paragraphs[0].font.size = Pt(20)
                    top += Inches(0.6)
    
    # 保存
    prs.save(output_path)
    return output_path


def create_quit_smoking_ppt():
    """创建戒烟成就 PPT"""
    
    slides_data = [
        {
            'title': '📊 戒烟成果统计',
            'content': [
                {'text': '戒烟开始日期：2025 年 12 月 14 日', 'level': 0, 'bold': True},
                {'text': '当前日期：2026 年 3 月 14 日', 'level': 0},
                {'text': '已戒烟：90 天', 'level': 0, 'bold': True, 'font_size': 24},
                {'text': '相当于：12 周 6 天', 'level': 1},
                {'text': '约：3 个月', 'level': 1},
            ]
        },
        {
            'title': '🏆 戒烟里程碑',
            'content': [
                {'text': '✅ 1 天 - 2025-12-15 - 完成！', 'level': 0},
                {'text': '✅ 7 天 - 2025-12-21 - 完成！', 'level': 0},
                {'text': '✅ 14 天 - 2026-01-01 - 完成！', 'level': 0},
                {'text': '✅ 30 天 - 2026-01-13 - 完成！', 'level': 0},
                {'text': '✅ 60 天 - 2026-02-12 - 完成！', 'level': 0},
                {'text': '🎉 90 天 - 2026-03-14 - 今天达成！', 'level': 0, 'bold': True},
                {'text': '⏳ 180 天 - 2026-06-12 - 还有 90 天', 'level': 0},
                {'text': '⏳ 365 天 - 2026-12-14 - 还有 275 天', 'level': 0},
            ]
        },
        {
            'title': '💪 健康收益',
            'type': 'table',
            'table_data': [
                ['时间', '身体变化', '状态'],
                ['20 分钟', '心率、血压恢复正常', '✅'],
                ['12 小时', '血液一氧化碳水平正常', '✅'],
                ['2 周 -3 个月', '肺功能开始改善', '✅'],
                ['90 天', '纤毛恢复，感染风险降低', '🎯 当前'],
                ['1 年', '冠心病风险减半', '⏳ 进行中'],
                ['5 年', '中风风险降至非吸烟者水平', '⏳ 未来'],
            ]
        },
        {
            'title': '💰 经济收益',
            'content': [
                {'text': '假设每天 1 包烟 (¥25/包)', 'level': 0, 'bold': True},
                {'text': '90 天节省：¥2,250', 'level': 0, 'font_size': 20},
                {'text': '1 年预计节省：¥9,125', 'level': 0, 'font_size': 20},
                {'text': '5 年预计节省：¥45,625', 'level': 0, 'font_size': 20},
                {'text': '10 年预计节省：¥91,250', 'level': 0, 'font_size': 20},
            ]
        },
        {
            'title': '🎊 恭喜主人！',
            'content': [
                {'text': '90 天不吸烟，主人已经战胜了自己！', 'level': 0, 'font_size': 24, 'bold': True},
                {'text': '平均戒烟成功率只有 3-5%', 'level': 1},
                {'text': '主人已经走在成功的路上！', 'level': 1},
                {'text': '', 'level': 0},
                {'text': '继续坚持，下一个目标：180 天！', 'level': 0, 'font_size': 24, 'bold': True},
                {'text': '', 'level': 0},
                {'text': '小诸 AI 助手 为主人加油！💪', 'level': 0, 'font_size': 20},
            ]
        },
    ]
    
    output_path = '/root/.openclaw/workspace/tmp/戒烟成就报告.pptx'
    create_presentation('🚭 主人戒烟成就报告', slides_data, output_path)
    return output_path


if __name__ == '__main__':
    import sys
    if len(sys.argv) > 1:
        ppt_type = sys.argv[1]
        if ppt_type == 'quit_smoking':
            path = create_quit_smoking_ppt()
            print(f'✅ PPTX 生成成功：{path}')
    else:
        # 默认生成示例 PPT
        path = create_quit_smoking_ppt()
        print(f'✅ PPTX 生成成功：{path}')
