<?php

class Reader
{

    private $table;
    private $primary_key;
    private $db_handler;

    public function __construct($table, $dashboard_connection, $primary_key = "id")
    {
        $this->table       = $table;
        $this->primary_key = $primary_key;
        $this->db_handler  = $dashboard_connection;
    }

    public function select_all()
    {

        $query = "select * from `" . $this->table . "`;";

        return $this->db_handler->query($query);
    }

/* get all names in a table in an assoc array that has its index as the IDs */
    /* return is an Asoc array keys are IDs and values are names */

    public function select_all_names_and_Ids($column_name = "name", $id_column = "id", $soft_delete = false)
    {
        $query = "select DISTINCT `" . $id_column . "`,`" . $column_name . "` from `" . $this->table . "`";

        if ($soft_delete) {
            $query .= " Where `" . column_deleted_at . "` is NULL";
        }

        $names   = array();
        $results = $this->db_handler->query($query);

        if (is_array($results)) {

            foreach ($results as $record) {
                $names[$record[0]] = $record[1];
            }

        }

        return $names;
    }

/* get an id OR a  whole record by filtering a table by a textual column inside it */
    /* return is one ID or One record */

    public function filter_by_column($column, $column_value, $only_id = false, $id = "id")
    {

        if (!$only_id) {
            $query = "select * from `" . $this->table . "` where `" . $column . "` = ?;";
        } else {
            $query = "select DISTINCT `" . $id . "` from `" . $this->table . "`  where `" . $column . "` = ?;";
        }

        $results = $this->db_handler->query($query, 'ASSOC', array($column_value));

        if (is_array($results)) {
            return $results;
        } else {
            return array();
        }

    }

    public function get_last_report_id($module_id, $id = "id")
    {

        $query = "select `" . $id . "` from `" . $this->table . "` where `module_id` = ?  ORDER BY`" . "id` DESC LIMIT 1 ;";

        $results = $this->db_handler->query($query, 'ASSOC', array($module_id));

        if (is_array($results)) {
            return $results;
        } else {
            return array();
        }

    }

/* get certain property value by filteriing table by id */
    /* result is one value from one record */

    public function get_property_by_id($required_property_column_name, $id_value, $id_column, $second_condition = "")
    {
        $query = "select DISTINCT `" . $required_property_column_name . "` from `" . $this->table . "`  where `" . $id_column . "` = ? ;";

        if (!empty($second_condition)) {
            $query .= " and $second_condition";
        }

        $result = $this->db_handler->query($query, 'ASSOC', array($id_value));

        if (isset($result[0][$required_property_column_name])) {
            return $result[0][$required_property_column_name];
        } else {
            srm_dashboard::log('Error in query', __FILE__ . "  lINE NO:" . __LINE__, __METHOD__, "", log_error, "report_builder_common", $query, json_encode(array("required_property" => $required_property_column_name, "required_id" => $id_value, "id_column" => $id_column, "second_condition" => $second_condition)));
            return false;
        }

    }

/* use to get a forign key IDs by filtering a column */
    /* result is array of Ids from array of records */

    public function get_filtered_ids($filter_by_column, $filter_by_value, $required_column)
    {
        $query  = "select DISTINCT `" . $required_column . "` from `" . $this->table . "`  where `" . $filter_by_column . "` = ?;";
        $result = $this->db_handler->query($query, 'ASSOC', array($filter_by_value));

        if (array($result) && !empty($result)) {
            $filtered_ids = array();

            foreach ($result as $record) {
                $filtered_ids[] = $record[$required_column];
            }

            return $filtered_ids;
        } else {
            return array();
        }

    }

    public function is_entity_exist_by_value($column, $value)
    {
        $query  = "select DISTINCT `" . $column . "` from `" . $this->table . "`  where `" . $column . "` = ?;";
        $result = $this->db_handler->query($query, 'ENUM', array($value));

        if (is_array($result) && !empty($result)) {

            return true;
        } else {
            return false;
        }

    }

}
