<?php

if (!defined('ABSPATH')) exit; // Exit if accessed directly

/**
 * Generate or regenerate the customer receipt PDF for a voucher.
 *
 * @param int $voucher_id
 * @return bool True on success, false otherwise
 */
function wpgv_generate_receipt_pdf_for_voucher($voucher_id)
{
    global $wpdb;

    $voucher_table = $wpdb->prefix . 'giftvouchers_list';
    $setting_table = $wpdb->prefix . 'giftvouchers_setting';

    $setting_options = $wpdb->get_row("SELECT * FROM $setting_table WHERE id = 1");
    $voucher = $wpdb->get_row($wpdb->prepare("SELECT * FROM $voucher_table WHERE id = %d", $voucher_id));

    if (!$voucher) {
        return false;
    }

    // Variables expected by templates/pdfstyles/receipt.php
    $for = isset($voucher->from_name) ? $voucher->from_name : '';
    $from = isset($voucher->to_name) ? $voucher->to_name : '';
    $email = ($voucher->shipping_email != '') ? $voucher->shipping_email : $voucher->email;
    $value = isset($voucher->amount) ? $voucher->amount : 0;
    $code = isset($voucher->couponcode) ? $voucher->couponcode : '';
    $expiry = isset($voucher->expiry) ? $voucher->expiry : '';
    $paytext = isset($voucher->pay_method) ? $voucher->pay_method : '';
    $lastid = $voucher_id;
    $voucher_options = $voucher; // keep name used in templates
    $payment_status = isset($voucher->payment_status) ? $voucher->payment_status : __('Not Paid', 'gift-voucher');

    $upload = wp_upload_dir();
    $upload_dir = $upload['basedir'];
    $receiptupload_dir = $upload_dir . '/voucherpdfuploads/' . $voucher->voucherpdf_link . '-receipt.pdf';

    // Generate receipt PDF using same template as initial creation
    $receipt = wpgv_create_pdf_safe('P', 'pt', array(595, 900));
    if ($receipt) {
        require_once(WPGIFT__PLUGIN_DIR . '/templates/pdfstyles/receipt.php');
    }

    $wpgv_enable_pdf_saving = get_option('wpgv_enable_pdf_saving') ? get_option('wpgv_enable_pdf_saving') : 0;
    if ($wpgv_enable_pdf_saving) {
        $receipt->Output($receiptupload_dir, 'F');
    } else {
        $receipt->Output('F', $receiptupload_dir);
    }

    return true;
}
