
@router.get("/{template_id}")
async def get_template(template_id: int, user: User = Depends(get_current_user),
                       db: AsyncSession = Depends(get_db)):
    t = await db.get(EmailTemplate, template_id)
    if not t or t.user_id != user.id:
        raise HTTPException(404, "Not found")
    analysis = analyze_template_variables(t.body_html)
    return {"id": t.id, "name": t.name, "subject_hint": t.subject_hint,
            "body_html": t.body_html, "body_text": t.body_text,
            "variables": analysis["variables"], "unknown_variables": analysis["unknown"]}


@router.put("/{template_id}")
async def edit_template(template_id: int, payload: TemplateIn,
                        user: User = Depends(get_current_user_csrf), db: AsyncSession = Depends(get_db)):
    t = await db.get(EmailTemplate, template_id)
    if not t or t.user_id != user.id:
        raise HTTPException(404, "Not found")
    if re.search(r"<script", payload.body_html, re.I):
        raise HTTPException(400, "Script tags are not allowed")
    t.name = payload.name[:250]
    t.body_html = payload.body_html
    t.body_text = payload.body_text
    t.subject_hint = payload.subject_hint[:500]
    await db.commit()
    return {"ok": True}


@router.post("/{template_id}/duplicate")
async def duplicate_template(template_id: int, user: User = Depends(get_current_user_csrf),
=====subjects PUT=====
    buf.seek(0)
    return StreamingResponse(iter([buf.getvalue()]), media_type="text/csv",
                             headers={"Content-Disposition": "attachment; filename=subjects.csv"})


@router.put("/{subject_id}")
async def edit_subject(subject_id: int, payload: SubjectIn,
                       user: User = Depends(get_current_user_csrf), db: AsyncSession = Depends(get_db)):
    row = await db.get(SubjectLine, subject_id)
    if not row or row.user_id != user.id:
        raise HTTPException(404, "Not found")
    row.subject = payload.subject.strip()[:500]
    await db.commit()
    return {"ok": True}


@router.delete("/{subject_id}")
async def delete_subject(subject_id: int, user: User = Depends(get_current_user_csrf),
                         db: AsyncSession = Depends(get_db)):
    row = await db.get(SubjectLine, subject_id)
    if not row or row.user_id != user.id:
        raise HTTPException(404, "Not found")
    await db.delete(row)
    await db.commit()
    return {"ok": True}

=====gmail PATCH=====
229:@router.patch("/accounts/{account_id}")
