from pathlib import Path
import importlib.util,json,sys,shutil
from reportlab.pdfgen import canvas
from reportlab.lib.colors import HexColor
from reportlab.platypus import Paragraph
from reportlab.lib.styles import ParagraphStyle
from xml.sax.saxutils import escape

HERE=Path(__file__).resolve().parent
ROOT=HERE.parents[6]
OUT=ROOT/'oss/local-deliverables/2026-09-23-AI智慧健康站一页纸-v3-品牌发布版'
spec=importlib.util.spec_from_file_location('v2',HERE.parent/'v2/build.py')
v2=importlib.util.module_from_spec(spec);spec.loader.exec_module(v2)
DATA=json.loads((HERE/'content.json').read_text())
W,H=595.276,841.89
BG='#FAF9F6';INK='#151A1D';GRAY='#667076';ORANGE='#EB6429';WHITE='#FFFFFF'
BOUNDS=[]

def box(c,x,y,w,h,col):
 c.setFillColor(HexColor(col));c.rect(x,H-y-h,w,h,fill=1,stroke=0)
def line(c,x,y,w,col='#DFE2DF'):
 c.setStrokeColor(HexColor(col));c.setLineWidth(.6);c.line(x,H-y,x+w,H-y)
def txt(c,t,x,y,w,s=10,col=INK,b=False,lead=None):
 p=Paragraph(escape(t).replace('\n','<br/>'),ParagraphStyle('p',fontName='CPTBold' if b else 'CPT',fontSize=s,leading=lead or s*1.5,textColor=HexColor(col),wordWrap='CJK'))
 _,h=p.wrap(w,H);assert y+h<H-15,(t,y,h)
 p.drawOn(c,x,H-y-h);BOUNDS.append({'text':t,'x':x,'y':y,'w':w,'h':h})
def base(c,kind):
 box(c,0,0,W,H,BG)
 c.drawImage(str(OUT/'qa/cpt-logo.png'),34,H-43,width=103,height=16.02,mask='auto')
 txt(c,'康比特 AI 智慧健康站',304,27,257,11,INK,True)
 txt(c,kind,34,62,220,8,GRAY)
def hero(c,image,lines,label,sub):
 box(c,0,81,W,446,'#090B0E')
 c.drawImage(str(image),0,H-527,width=W,height=397,mask='auto')
 txt(c,label,34,100,527,10,'#FBB98D',True)
 txt(c,lines,34,130,527,31,WHITE,True,lead=43)
 if image.name=='product-hero.png':
  c.saveState();c.setFillColor(HexColor('#090B0E'));c.setFillAlpha(.42);c.rect(28,H-265,290,40,fill=1,stroke=0);c.restoreState()
 txt(c,sub,34,230,527,10,'#DBDFE1',False,lead=15)
 c.saveState();c.setFillColor(HexColor('#090B0E'));c.setFillAlpha(.65);c.rect(470,H-517,95,17,fill=1,stroke=0);c.restoreState()
 txt(c,'品牌概念视觉',477,503,93,7,'#E2E5E7')
def notes(c,note):
 line(c,34,785,527)
 txt(c,note,34,797,527,7,GRAY,lead=10)

def product(c,d):
 base(c,'产品介绍  /  PRODUCT')
 hero(c,OUT/'assets/product-hero.png','把国家队的营养智慧，\n带进每个人的日常。','AI运动营养师','健康体检体测一体机 + AI运动营养师\n让身体数据，成为每一天的吃动答案。')
 for x,num,desc in [(34,'25年','运动营养专业积累'),(222,'100支','国家队服务经验'),(410,'百款硬件','连接运动健康生态')]:
  txt(c,num,x,548,153,25,INK,True)
  txt(c,desc,x,589,153,9,GRAY)
 txt(c,'专业经验进入AI，个人数据形成方案。',34,628,527,17,INK,True)
 txt(c,'以长期运动营养经验训练营养垂类大模型，结合体测与日常记录，\n生成运动、营养两份个人方案；通过执行反馈与复测，支持持续调整。',34,663,527,10,GRAY,lead=16)
 txt(c,'华为运动健康  /  苹果健康生态  /  支持的硬件与个人记录',34,708,527,9,GRAY)
 line(c,34,735,527)
 txt(c,'从一次检测开始，开启AI运动营养体验。',34,751,527,12,ORANGE,True)
 notes(c,d['note'])

def channel(c,d):
 base(c,'渠道合作  /  PARTNERS')
 hero(c,OUT/'assets/channel-hero.png','让专业营养，\n成为你的下一项健康业务。','康比特AI智慧健康站 · 渠道合作','你熟悉客户，我们提供专业营养产品。\n一起，把AI健康服务带到更多人身边。')
 txt(c,'一体机  +  AI运动营养师  +  可选营养品',34,547,527,16,INK,True)
 txt(c,'25年专业积累，100支国家队服务经验。\n以AI连接现场检测与日常吃动服务，匹配门店、机构和企业的真实需求。',34,580,527,10,GRAY,lead=16)
 for x,title,body in [(34,'设备销售','通过设备销售，\n按约定获得供销差价。'),(311,'营养品复购','以真实服务与需求承接订单，\n按约定规则形成合作收益。')]:
  txt(c,title,x,632,250,20,INK,True)
  txt(c,body,x,671,250,10,GRAY,lead=16)
 txt(c,'健身与瑜伽门店  /  健康管理与体检机构  /  企业员工福利',34,719,527,9,GRAY)
 line(c,34,742,527)
 txt(c,'从一个场景开始，共同拓展专业营养健康服务。',34,755,527,11.5,ORANGE,True)
 notes(c,d['note'])

def main():
 (OUT/'qa').mkdir(parents=True,exist_ok=True)
 v2.OUT=OUT;v2.QA=OUT/'qa';v2.HERE=HERE;v2.DATA=DATA
 v2.build_text()
 shutil.copy2(ROOT/'oss/local-deliverables/2026-09-23-AI智慧健康站一页纸-v2-微软雅黑/qa/cpt-logo.png',OUT/'qa/cpt-logo.png')
 if '--text-only' in sys.argv:return
 target=OUT/'02-图文版';target.mkdir(exist_ok=True)
 for d,draw in zip(DATA,[product,channel]):
  c=canvas.Canvas(str(target/(d['file']+'_品牌发布版.pdf')),pagesize=(W,H),initialFontName='CPT',initialFontSize=10)
  c.setTitle(d['title']+' '+d['subtitle']);c.setAuthor('康比特');draw(c,d);c.showPage();c.save()
 (OUT/'qa/layout-bounds.json').write_text(json.dumps(BOUNDS,ensure_ascii=False,indent=2))

if __name__=='__main__':main()
