<?php

namespace objects;

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

class customer_notes_o extends db
{
    use db_object_t;

    public object_property $customer_id;
    public object_property $note;
    public object_property $cashier_id;
    public object_property $deleted_at;

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

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

    public function add(int $customer_id, string $note, int $cashier_id): customer_notes_o
    {
        global $db, $response;
        try {
            // Avoid SQL injection
            $note = $db->escape_string($note);
            // Create a new record in the database
            $sql = "INSERT INTO $this->table (customer_id, note, cashier_id) VALUES ($customer_id, '$note', $cashier_id)";
            $db->query($sql);

            // Get the id of the new record
            $this->id = $db->insert_id();

            // Set the values of the object properties
            $this->getObjectProperties();

            // Clear the cache
            redis->clear_customer_notes($customer_id);
        } catch (\Exception $e) {
            $response->error($e->getMessage());
        }
        return $this;
    }

    public function getObjectProperties(): void
    {
        $this->customer_id = new object_property($this->table, $this->id, 'customer_id', 'int', true);
        $this->note = new object_property($this->table, $this->id, 'note', 'string', true);
        $this->cashier_id = new object_property($this->table, $this->id, 'cashier_id', 'int', true);
        $this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
    }

    public function getCustomerNotesAsArray(int $user_id): array
    {
        global $response, $db;
        // Check if the result is cached
        $cached = redis->get_customer_notes($user_id);
        if ($cached) {
            $response->add_meta('cached', true);
            return $cached;
        }
        $sql = "SELECT * FROM $this->table WHERE customer_id = $user_id AND deleted_at IS NULL";
        $result = $db->query($sql);
        $customer_notes = [];
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $customer_notes[] = $row;
            }
        }
        // Cache the result
        redis->cache_customer_notes($user_id, $customer_notes);
        return $customer_notes;
    }

    public function getCustomerNoteById(int $id): customer_notes_o
    {
        global $db;
        // Get the record from the database
        $sql = "SELECT * FROM $this->table WHERE id = $id";
        $result = $db->query($sql);
        if ($result->num_rows > 0) {
            $this->id = $id;
            $this->getObjectProperties();
        }
        return $this;
    }

    public function delete(int $id): void
    {
        global $db;
        $this->id = $id;
        $sql = "UPDATE $this->table SET deleted_at = NOW() WHERE id = $this->id";
        $db->query($sql);
        // Clear the cache
        $customer_id = $this->getCustomerByNoteId($this->id);
        redis->clear_customer_notes($customer_id);
    }

    public function getCustomerByNoteId(int $id): int
    {
        global $db;
        $sql = "SELECT customer_id FROM $this->table WHERE id = $id";
        $result = $db->query($sql);
        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            return $row['customer_id'];
        }
        return 0;
    }

    public function restore(int $id): void
    {
        global $db;
        $this->id = $id;
        $sql = "UPDATE $this->table SET deleted_at = NULL WHERE id = $this->id";
        $db->query($sql);
        // Clear the cache
        $customer_id = $this->getCustomerByNoteId($this->id);
        redis->clear_customer_notes($customer_id);
    }

    public function asArray(): array
    {
        return [
            'id' => $this->id,
            'customer_id' => $this->customer_id->value(),
            'note' => $this->note->value(),
            'cashier_id' => $this->cashier_id->value(),
            'deleted_at' => $this->deleted_at->value(),
        ];
    }
}