<?php

namespace objects;

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

class department_selfserve_conditions_o extends db
{
    use db_object_t;

    /**
     * Canonical relationship model:
     * - A condition is a reusable logical node.
     * - `condition_id` on this entity is a parent condition id, enabling nested condition trees.
     * - Questions may reference a condition as their gate.
     * - Tasks may reference a question as their gate (legacy stored in tasks.condition_id).
     */

    public object_property $department; // The department id
    public object_property $lane; // The lane id
    public object_property $product; // The product id
    public object_property $machine_type_id; // The reusable machine type id (nullable, preferred over department/lane/product)
    public object_property $condition_id; // Parent condition id for nesting/grouping (nullable)
    public object_property $name; // The condition name
    public object_property $description; // The task description
    public object_property $created_at;
    public object_property $updated_at;
    public object_property $deleted_at;


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

    /**
     * Add a condition
     * @param int $department The department id
     * @param int $lane The lane id
     * @param int $product The product id
     * @param string $name The condition name
     * @param string $description The condition description
     * @param int|null $condition_id Optional parent condition id for nesting/grouping.
     * @return department_selfserve_conditions_o
     * @throws Exception If the object was not created successfully
     */
    public function add(int $department, int $lane, int $product, string $name, string $description, int $condition_id = null, ?int $machine_type_id = null): department_selfserve_conditions_o
    {
        global /** @var db $db */
        $db;
        // Sanitize the input
        $department = (int)$department;
        $lane = (int)$lane;
        $product = (int)$product;
        $name = $db->escape_string($name);
        $description = $db->escape_string($description);
        if (!is_null($condition_id)) {
            $condition_id = (int)$condition_id;
        }
        if (!is_null($machine_type_id)) {
            $machine_type_id = (int)$machine_type_id;
        }

        // Add the object
        $tmp_id = self::add_object([
            'department' => $department,
            'lane' => $lane,
            'product' => $product,
            ...(!is_null($machine_type_id) ? ['machine_type_id' => $machine_type_id] : []),
            'name' => $name,
            'description' => $description,
            ...(!is_null($condition_id) ? ['condition_id' => $condition_id] : []),
        ]);
        $this->id = $tmp_id;
        self::getObjectProperties();
        self::objectChanged();
        return $this;
    }



    public function getObjectProperties(): void
    {
        $this->department = new object_property($this->table, $this->id, 'department', 'int', false);
        $this->lane = new object_property($this->table, $this->id, 'lane', 'int', false);
        $this->product = new object_property($this->table, $this->id, 'product', 'int', false);
        $this->machine_type_id = new object_property($this->table, $this->id, 'machine_type_id', 'int', false);
        $this->condition_id = new object_property($this->table, $this->id, 'condition_id', 'int', false);
        $this->name = new object_property($this->table, $this->id, 'name', 'string', false);
        $this->description = new object_property($this->table, $this->id, 'description', 'string', false);
        $this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', false);
        $this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', false);
        $this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
    }

    public function objectChanged(): void
    {
        //TODO: Add cache invalidation
    }

    public function asArray(): array
    {
        return [
            'id' => (int)$this->id,
            'department' => (int)$this->department->value(),
            'lane' => (int)$this->lane->value(),
            'product' => (int)$this->product->value(),
            'machine_type_id' => is_null($this->machine_type_id->value()) ? null : (int)$this->machine_type_id->value(),
            'condition_id' => is_null($this->condition_id->value()) ? null : (int)$this->condition_id->value(),
            'name' => (string)$this->name->value(),
            'description' => (string)$this->description->value(),
            // Timestamps
            'created_at' => (string)$this->created_at->value(),
            'updated_at' => (string)$this->updated_at->value(),
        ];
    }

    public function getConditionsForMachineType(int $machineTypeId): array
    {
        return self::getFieldsWhere([
            'machine_type_id' => $machineTypeId,
            'deleted_at' => null,
        ], ['id', 'department', 'lane', 'product', 'machine_type_id', 'condition_id', 'name', 'description', 'created_at', 'updated_at']);
    }

    public function getLegacyConditionsForLaneProduct(int $departmentId, int $laneId, int $productId): array
    {
        return self::getFieldsWhere([
            'department' => $departmentId,
            'lane' => $laneId,
            'product' => $productId,
            'deleted_at' => null,
        ], ['id', 'department', 'lane', 'product', 'machine_type_id', 'condition_id', 'name', 'description', 'created_at', 'updated_at']);
    }
}
