/*
 * hello_docx.c — SmartScribe.Docx SDK quick-start example
 *
 * Demonstrates:
 *   - Opening an existing DOCX
 *   - Reading and modifying margins
 *   - Setting document properties
 *   - Building and writing a header/footer
 *   - Saving
 *
 * Build (Windows, MSVC):
 *   cl hello_docx.c /I../../include SmartScribeDocx.lib
 *
 * Build (GCC/Clang):
 *   gcc hello_docx.c -I../../include -L../../prebuilt/win-x64 -lSmartScribeDocx -o hello_docx
 */

#include <stdio.h>
#include "SmartScribeDocx.h"

int main(void)
{
    /* Initialize (required when no Qt event loop exists) */
    ssDocx_init();

    printf("SmartScribe.Docx SDK %s\n", ssDocx_version());

    /* Open an existing document */
    SsDocxHandle h = ssDocx_open("sample.docx");
    if (!h) {
        fprintf(stderr, "Failed to open: %s\n", ssDocx_error(NULL));
        return 1;
    }

    /* Read current margins (values in twips; 1440 twips = 1 inch) */
    int top, bottom, left, right, hd, fd, gutter, mirror;
    ssDocx_get_margins(h, &top, &bottom, &left, &right, &hd, &fd, &gutter, &mirror);
    printf("Current margins: top=%d bottom=%d left=%d right=%d\n",
           top, bottom, left, right);

    /* Set 1-inch margins all around */
    ssDocx_set_margins(h, 1440, 1440, 1440, 1440, 709, 709, 0, 0);

    /* Set document properties */
    ssDocx_set_doc_property(h, "title",  "My Document");
    ssDocx_set_doc_property(h, "author", "Dan Shields");

    /* Build a header: "My Document" on left, page number on right */
    unsigned int hdr_size = 0;
    const unsigned char* hdr_xml =
        ssDocx_build_hf_xml(1, "{TITLE}", "left", &hdr_size);

    unsigned int pgnum_size = 0;
    const unsigned char* pgnum_xml =
        ssDocx_build_hf_xml(1, "Page {PAGE} of {NUMPAGES}", "right", &pgnum_size);

    /* Write both (set the same header twice for left and right alignment
       is not how DOCX works — normally one header part has left/center/right
       tabs. This demo shows the API; see the H/F builder docs for full layout.) */
    ssDocx_set_header_xml(h, SS_HF_DEFAULT, hdr_xml, hdr_size);

    /* Build and write a footer */
    unsigned int ftr_size = 0;
    const unsigned char* ftr_xml =
        ssDocx_build_hf_xml(0, "{AUTHOR}  |  Confidential", "center", &ftr_size);
    ssDocx_set_footer_xml(h, SS_HF_DEFAULT, ftr_xml, ftr_size);

    /* Save to a new file */
    if (!ssDocx_save_as(h, "output.docx")) {
        fprintf(stderr, "Save failed: %s\n", ssDocx_error(h));
        ssDocx_close(h);
        return 1;
    }

    printf("Saved to output.docx\n");
    ssDocx_close(h);
    return 0;
}
