<?php

namespace endpoints\orders;

use traits\economic_endpoint_t;

class economic_invoices_pdf_endpoint
{
    use economic_endpoint_t;

    /**
     * Get a PDF of a booked invoice.
     *
     * Kept as the legacy alias used by existing invoice download flows.
     * @param int $id
     * @return string
     */
    public function get(int $id): string
    {
        return $this->getBooked($id);
    }

    /**
     * Get a PDF of a booked invoice.
     * @param int $id
     * @return string
     */
    public function getBooked(int $id): string
    {
        return $this->downloadPdf('/invoices/booked/' . $id . '/pdf', 'booked_invoice_');
    }

    /**
     * Get a PDF of a draft invoice.
     * @param int $id
     * @return string
     */
    public function getDraft(int $id): string
    {
        return $this->downloadPdf('/invoices/drafts/' . $id . '/pdf', 'draft_invoice_');
    }

    private function downloadPdf(string $path, string $prefix): string
    {
        $unique_id = uniqid();
        $file_path = '/tmp/' . $prefix . $unique_id . '.pdf';
        $this->send_file_download_request(
            $path,
            'GET',
            '',
            false,
            $file_path
        );
        return $file_path;
    }

}
