<?php

namespace objects;

use classes\db;
use classes\object_property;
use traits\db_object_t;

class economic_module_orders extends db
{
    use db_object_t;

    public object_property $economic_invoice_draft_id;
    public object_property $economic_invoice_id;

    public function structure(): void
    {
        $this->setTable('economic_module_orders');
    }

    public function objectChanged(): void
    {
        // Since the economic_module_orders object is not cached, there is no need to invalidate the cache
    }

    public function getByOrderId(int $orderId): economic_module_orders
    {
        global $db;
        // Create a new record in the database, if it does not exist
        $sql = "SELECT * FROM $this->table WHERE id = $orderId";
        $result = $db->query($sql);
        if ($result->num_rows > 0) {
            $this->id = $orderId;
            $this->getObjectProperties();
        } else {
            $this->add($orderId);
        }
        return $this;
    }

    public function getObjectProperties(): void
    {
        $this->economic_invoice_draft_id = new object_property($this->table, $this->id, 'invoice_draft_id', 'int', true);
        $this->economic_invoice_id = new object_property($this->table, $this->id, 'invoice_id', 'int', true);
    }

    public function add(int $orderId): void
    {
        global $db, $response;
        try {
            // Create a new record in the database
            $sql = "INSERT INTO $this->table (id) VALUES ($orderId)";
            $db->query($sql);

            // Get the id of the new record
            $this->id = $orderId;

            // Set the values of the object properties
            $this->getObjectProperties();
        } catch (\Exception $e) {
            $response->error($e->getMessage());
        }
    }

    public function unlinkAllOrdersFromDraft(int $draftId): void
    {
        global $db;
        $sql = "UPDATE $this->table SET invoice_draft_id = NULL WHERE invoice_draft_id = $draftId";
        $db->query($sql);
    }

    public function asArray(): array
    {
        return [
            'id' => $this->id,
            'invoice_draft_id' => $this->economic_invoice_draft_id->value(),
            'invoice_id' => $this->economic_invoice_id->value(),
        ];
    }

    public function getUserFromDraftId(int $id): users_o|null
    {
        global $db;
        $sql = "SELECT id FROM $this->table WHERE invoice_draft_id = $id";
        $result = $db->query($sql);
        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            $orderId = $row['id'];
            $order = new orders_o();
            return $order->getOrderCustomer($orderId);
        }
        return null;
    }
}