<?php

namespace objects;

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

class customer_fixed_pricing_o extends db
{
    use db_object_t;

    public object_property $customer_number; // The customer number (E-conomic)
    public object_property $price;      // The price (DKK) of the monthly fixed-price
    public object_property $description;     // The description of the agreement
    public object_property $created_at; // The timestamp of when the record was created
    public object_property $updated_at; // The timestamp of when the record was updated

    /**
     * Convert the object to an array
     * @return array
     * @throws Exception If the object is not selected
     */
    public function asArray(): array
    {
        self::requireSelected();
        return [
            'id' => (int)$this->id,
            'customer_number' => (int)$this->customer_number->value(),
            'price' => (int)$this->price->value(),
            'description' => (string)$this->description->value(),
            'created_at' => (string)$this->created_at->value(),
            'updated_at' => (string)$this->updated_at->value(),
        ];
    }


    /**
     * @throws Exception If the creation of the object fails
     */
    public function add(int $customer_number, int $price, string $description = ''): void
    {
        global $db, $response;
        $tmp_id = self::add_object([
            'customer_number' => (int)$customer_number,
            'price' => (int)$price,
            'description' => (string)$description,
        ]);
        self::select((int)$tmp_id);
        self::objectChanged();
    }

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

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

    public function getObjectProperties(): void
    {
        $this->customer_number = new object_property($this->table, $this->id, 'customer_number', 'int', true);
        $this->price = new object_property($this->table, $this->id, 'price', 'int', true);
        $this->description = new object_property($this->table, $this->id, 'description', 'string', true);
        $this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', true);
        $this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', true);
    }

    /**
     * Get the customer_fixed_pricing object by customer number
     * @param int $customer_number
     * @return bool Returns true if the customer_fixed_pricing object exists, false otherwise.
     */
    public function doesUserHaveFixedPricing(int $customer_number): bool
    {
        return $this->getIdByCustomerNumber($customer_number) !== null;
    }

    /**
     * Get the ID of the customer_fixed_pricing object by customer number
     * @param int $customer_number
     * @return int|null Returns the ID of the customer_fixed_pricing object, if it exists. Returns null, if it does not exist.
     */
    public function getIdByCustomerNumber(int $customer_number): ?int
    {
        $matches = self::getFieldsWhere([
            'customer_number' => (int)$customer_number,
        ], [
            'id'
        ]);
        if (count($matches) > 0) {
            // There is a match, so return the ID
            return (int)$matches[0]['id'];
        } else {
            // There's no match, so return null
            return null;
        }
    }

    /**
     * Get the customer_fixed_pricing object by customer number
     * @param int $customer_number
     * @return customer_fixed_pricing_o|null Returns the customer_fixed_pricing object, if it exists. Returns null, if it does not exist.
     * @throws Exception If the user does not have fixed pricing, or if the object is not selected successfully.
     * @see doesUserHaveFixedPricing()
     * @see getIdByCustomerNumber()
     */
    public function selectByCustomerNumber(int $customer_number): ?customer_fixed_pricing_o
    {
        $this->select($this->getIdByCustomerNumber($customer_number));
        if ($this->exists()) {
            return $this;
        } else {
            throw new Exception('Unable to select fixed pricing item by customer_number, no item found');
        }
    }
}