<?php

class economic_invoice_draft_mo extends economicInvoicesDrafts
{
    protected int $customer_number;
    protected string $date; // Date of the invoice (YYYY-MM-DD)
    protected string $currency; // Currency of the invoice (DKK, EUR, USD, etc.)
    protected float $layout_number; // Layout number of the invoice
    protected float $payment_terms_number; // Payment terms number of the invoice
    protected array $recipient; // Recipient of the invoice (Includes name, address, zip, city, and (array)vatZone)
    protected array $lines; // Lines of the invoice (Includes product, quantity, unitNetPrice, discountPercentage, and (array)vatRate)

    public function addLine(string $productNumber, string $description, float $quantity, float $unitNetPrice, float $discountPercentage, int $economic_department_id, int $dimension): void
    {
        // We then add a 0 in front of the product number to make sure it's the right one, made by FlexPOS.
        // Add a line to the invoice
        $line = [
            'product' => [
                'productNumber' => $productNumber,
            ],
            'quantity' => $quantity,
            'unitNetPrice' => $unitNetPrice,
            'discountPercentage' => $discountPercentage,
            'description' => $description,
        ];
        // If the department is set, add it to the line
        if ($economic_department_id) {
            $line['departmentalDistribution'] = [
                'departmentalDistributionNumber' => $economic_department_id,
                'DistributionType' => 'Department',
                'dimension' => $dimension
            ];
        }
        $this->lines[] = $line;
    }

    public function addLineTEXT(string $text): void
    {
        // Add a line to the invoice
        $this->lines[] = [
            'description' => (string)$text,
        ];
    }

    public function createInvoiceDraftExample(): object
    {
        $data = [
            'currency' => 'DKK',
            'date' => date('Y-m-d'),
            'layout' => [
                'layoutNumber' => (int)$this->economic->config->invoice_layout->getVariableValue()
            ],
            'paymentTerms' => [
                'paymentTermsNumber' => 1
            ],
            'recipient' => [
                'name' => $this->recipient['name'],
                'address' => $this->recipient['address'],
                'zip' => $this->recipient['zip'],
                'city' => $this->recipient['city'],
                'vatZone' => [
                    'vatZoneNumber' => 1
                ]
            ],
            'customer' => [
                'customerNumber' => (int)$this->customer_number
            ],
            'lines' => $this->lines // Lines added using the addLine method
        ];
        return $this->createInvoiceDraft($data);
    }

    // Example of a method that uses the createInvoiceDraft method

    public function createInvoiceDraft(array $data): object
    {
        // Create a draft invoice
        $url = '/invoices/drafts';
        $response = $this->send_request($url, 'POST', json_encode($data));
        return json_decode($response);
    }

    public function setCustomerNumber(int $customer_number): economic_invoice_draft_mo
    {
        $this->customer_number = $customer_number;
        return $this;
    }

    public function setRecipient(string $name, string $address, string $zip, string $city): economic_invoice_draft_mo
    {
        $this->recipient = [
            'name' => $name,
            'address' => $address,
            'zip' => $zip,
            'city' => $city,
        ];
        return $this;
    }

    public function deleteInvoiceDraft(int $value): void
    {
        // Delete the invoice draft
        $url = '/invoices/drafts/' . $value;
        $this->send_request($url, 'DELETE');
    }

    public function publishInvoiceDraft(int $invoiceDraftId): object
    {
        // Publish the invoice draft
        $url = '/invoices/booked';
        return json_decode($this->send_request($url, 'POST', json_encode([
                'draftInvoice' => [
                    'draftInvoiceNumber' => $invoiceDraftId
                ]
            ])
        ));
    }

    public function getInvoicePdf(int $param)
    {
        // Get the invoice PDF
        $url = '/invoices/booked/' . $param . '/pdf';
        return $this->send_request($url, 'GET');
    }

    public function getInvoiceDraft(int $int): object
    {
        // Get the invoice draft
        $url = '/invoices/drafts/' . $int;
        return json_decode($this->send_request($url, 'GET'));
    }

    public function addLinesToInvoiceDraft(int $invoiceDraftId): object
    {
        // Add lines to the invoice draft
        $url = '/invoices/drafts/' . $invoiceDraftId . '/lines';
        return json_decode($this->send_request($url, 'POST', json_encode(['lines' => $this->lines])));
    }
}