<?php 

namespace App\Models;

use CodeIgniter\Model;

class Attributes extends Model
{
    /**
     * table users name
     * @var string
     */
    protected $table = 'attributes';
    protected $primaryKey = 'id';
    protected $allowedFields = ['name', 'active'];

    /**
     * get active attribute data
     * @return array
     */
    public function getActiveAttributeData()
    {
        return $this->where('active', 1)->findAll();
    }

    /**
     * get data by id or all
     * @param int $id
     * @return array|object|null
     */
    public function getAttributeData(int $id = null)
    {
        if ($id) {
            return $this->find($id); // Find by id
        }
        return $this->findAll(); // Get all
    }

    /**
     * Count attribute values
     * @param mixed $id
     * @return int|string
     */
    public function countAttributeValue($id = null)
    {
        if ($id) {
            return $this->builder('attribute_value')->where('attribute_parent_id', $id)->countAllResults();
        }
        return 0;
    }

    /**
     * Get attribute value data by attribute_parent_id
     * @param mixed $id
     * @return array
     */
    public function getAttributeValueData($id = null)
    {
        return $this->builder('attribute_value')->where('attribute_parent_id', $id)->get()->getResultArray();
    }

    /**
     * Get attribute value by id
     * @param mixed $id
     * @return array|null
     */
    public function getAttributeValueById($id = null)
    {
        return $this->builder('attribute_value')->where('id', $id)->get()->getRowArray();
    }

    /**
     * Insert attribute data
     * @param mixed $data
     * @return bool|int|string
     */
    public function create($data)
    {
        if ($data) {
            return $this->insert($data); // Returns true/false based on success
        }
        return false;
    }

    /**
     * Update attribute data
     * @param mixed $data
     * @param mixed $id
     * @return bool
     */
    public function updateAttribute($data, $id)
    {
        if ($data && $id) {
            return $this->update($id, $data); // Returns true/false based on success
        }
        return false;
    }

    /**
     * Remove attribute data
     * @param mixed $id
     * @return bool|\CodeIgniter\Database\BaseResult
     */
    public function remove($id)
    {
        if ($id) {
            return $this->delete($id); // Returns true/false based on success
        }
        return false;
    }

    /**
     * Insert attribute value
     * @param mixed $data
     * @return bool|\CodeIgniter\Database\BaseResult|\CodeIgniter\Database\Query
     */
    public function createValue($data)
    {
        if ($data) {
            return $this->builder('attribute_value')->insert($data); // Insert into attribute_value table
        }
        return false;
    }

    /**
     * Update attribute value
     * @param mixed $data
     * @param mixed $id
     * @return bool
     */
    public function updateValue($data, $id)
    {
        if ($data && $id) {
            return $this->builder('attribute_value')->where('id', $id)->update($data); // Update attribute_value table
        }
        return false;
    }

    /**
     * Remove attribute value
     * @param mixed $id
     * @return bool|string
     */
    public function removeValue($id)
    {
        if ($id) {
            return $this->builder('attribute_value')->where('id', $id)->delete(); // Delete from attribute_value table
        }
        return false;
    }
}