<?php

namespace Functions;

use Classes\Database;
use \DateTime;


class Sends extends Database
{

    public function add($values)
    {
        if (!is_array($values)) return FALSE;
        if (count($values) <> 13) return FALSE;

        $sql = "INSERT INTO sends (type,file_id,caption,exception,recivers_type,recivers,success,failed,created_time,filename,finished_time,status,admin) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
        $this->prepare($sql)->execute($values);
        return $this->lastInsertId();
    }
    public function add_send_history($values)
    {
        if (!is_array($values)) return FALSE;
        if (count($values) <> 3) return FALSE;

        $sql = "INSERT INTO sends_history (ad_id, userid, msgid) VALUES (?,?,?)";
        $this->prepare($sql)->execute($values);
        return $this->lastInsertId();
    }

    public function update($id, $changes)
    {
        if (!is_array($changes)) return FALSE;
        $keys_sql = "";
        $values = [];
        foreach ($changes as $key => $value) {
            $keys_sql .= "$key=?, ";
            $values[] = $value;
        }
        $keys_sql = substr($keys_sql, 0, -2);
        $sql = "UPDATE sends SET $keys_sql WHERE id=$id";
        $this->prepare($sql)->execute($values);
        return $this->lastInsertId();
    }

    public function calculator_recivers_count($type){

        $sql = "SELECT * FROM bot_users";
        $this->prepare($sql);
        $this->execute();
        return $this->rowCount();
    }


    public function get($where = NULL, $order_by = NULL, $offset = NULL, $limit = NULL)
    {
        $sql = "SELECT * FROM sends";

        if (!is_null($where) && !is_array($where)) return FALSE;
        if (is_array($where)) $sql .= " WHERE {$where[0]}=?";
        if (!is_null($order_by)) $sql .= " ORDER BY $order_by";
        if (!is_null($offset)) $sql .= " OFFSET $offset";
        if (!is_null($limit)) $sql .= " LIMIT $limit";

        $this->prepare($sql);
        if (is_array($where)) $this->execute([$where[1]]);
        $results = $this->resultStr();
        if (count($results) > 0) {
            return $results;
        } else {
            return false;
        }

    }

    public function remove($id)
    {
        $sql = "DELETE FROM sends WHERE id = ?";
        return $this->prepare($sql)->execute([$id]);
    }

    public function get_by_id($id)
    {
        return $this->get(['id',$id]);
    }

    public function get_progress_sends(){
        return $this->get(['status',0]);
    }

    public function get_progress_delete(){
        return $this->get(['status',2]);
    }

    public function get_users_to_send($id){
        $sql = "SELECT * FROM bot_users WHERE last_msg_get < ? LIMIT 100";
        $this->prepare($sql);
        $this->execute([$id]);
        return $this->resultStr();
    }
    public function get_msgs_to_delete($id){
        $sql = "SELECT * FROM sends_history WHERE ad_id = ? LIMIT 200";
        $this->prepare($sql);
        $this->execute([$id]);
        return $this->resultStr();
    }

    public function remove_send_history($id)
    {
        $sql = "DELETE FROM sends_history WHERE id = ?";
        return $this->prepare($sql)->execute([$id]);
    }

    public function search( $from_date = NULL, $to_date = NULL, $order_by = NULL, $offset = NULL, $limit = NULL)
    {
        $sql = "SELECT * FROM sends WHERE id > 0";
        $execute_array = [];


        if (!is_null($from_date)) {
            $sql .= " AND created_time >= ?";
            $execute_array[] = $from_date;
        }

        if (!is_null($to_date)) {
            $sql .= " AND created_time <= ?";
            $execute_array[] = $to_date;
        }


        if (!is_null($order_by)) $sql .= " ORDER BY $order_by";
        if (!is_null($limit)) $sql .= " LIMIT $limit";
        if (!is_null($offset)) $sql .= " OFFSET $offset";

        $this->prepare($sql);
        if (count($execute_array) > 0) {
            $this->execute($execute_array);
        } else {
            $this->execute();
        }

        $results = $this->resultStr();
        if (count($results) > 0) {
            return $results;
        } else {
            return false;
        }
    }

    public function search_count( $from_date = NULL, $to_date = NULL)
    {
        $sql = "SELECT * FROM sends WHERE id > 0";
        $execute_array = [];


        if (!is_null($from_date)) {
            $sql .= " AND created_time >= ?";
            $execute_array[] = $from_date;
        }

        if (!is_null($to_date)) {
            $sql .= " AND created_time <= ?";
            $execute_array[] = $to_date;
        }


        $this->prepare($sql);
        if (count($execute_array) > 0) {
            $this->execute($execute_array);
        } else {
            $this->execute();
        }

        return $this->rowCount();
    }

}