<commit_before><?php namespace Ascend\Core;

use Ascend\Core\Bootstrap;
use Ascend\Core\Request;
use Ascend\Core\Feature\Validation;

class Model
{

    protected $db;
    protected $table;
    protected static $dbStatic;

    /**
     * Non static functions below
     */

    public function __construct()
    {
        $this->db = Bootstrap::getDBPDO();

        if (isset($this->fillable) && is_array($this->fillable) && count($this->fillable) > 0) {
            foreach ($this->fillable AS $field) {
                $this->{$field} = null;
            }
        }
    }

    public function getTable()
    {
        return $this->table;
    }

    public function createTable()
    {

        $fields = $this->getStructure();

        $sqlFields = implode(', ', $fields);

        // @todo update this in framework; added default and COLLATE
        $sql = "
        CREATE TABLE IF NOT EXISTS `{$this->table}` ({$sqlFields})
        ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci
        ";

        $this->db->query($sql);
        $this->db->execute();

        $this->loadSeed();
    }

    public function loadSeed()
    {
        if (isset($this->seed) && is_array($this->seed) && count($this->seed) > 0) {
            foreach ($this->seed AS $k => $fields) {
                Bootstrap::getDB()->insert($this->table, $fields);
            }
        }
    }

    public function getFillable() {
        return $this->fillable;
    }

    public function getStructure()
    {
        $fields = array();
        foreach ($this->structure as $field => $type) {
            $fields[$field] = '`' . $field . '` ' . $type;
        }

        if (!isset($this->timestamps)){ $this->timestamps = true; }
        if ($this->timestamps === true) {
            $fields['created_at'] = 'created_at timestamp null default null';
            $fields['updated_at'] = 'updated_at timestamp null default null';
        }

        if (!isset($this->softDelete)){ $this->softDelete = true; }
        if ($this->softDelete === true) {
            $fields['deleted_at'] = 'deleted_at timestamp null default null';
        }

        return $fields;
    }

    public function getRenameMap() {
        return (isset($this->renameMap)?$this->renameMap:[]);
    }

    public static function insert($fields)
    {
        $fields['created_at'] = date('Y-m-d H:i:s', time());
        return Bootstrap::getDB()->insert(self::getTableName(), $fields);
    }

    // @todo move into framework
    public static function update($fields, $where) {
        $fields['updated_at'] = date('Y-m-d H:i:s', time());
        return Bootstrap::getDB()->update(self::getTableName(), $fields, $where);
    }

    /**
     * Inserts or Updates
     */
    public function save()
    {
        $modelName = $this->getCalledModelName();
        $tableName = $this->getTable();
        /*<commit_msg>Updated created_at, updated_at<commit_after><?php namespace Ascend\Core;

use Ascend\Core\Bootstrap;
use Ascend\Core\Request;
use Ascend\Core\Feature\Validation;

class Model
{

    protected $db;
    protected $table;
    protected static $dbStatic;

    /**
     * Non static functions below
     */

    public function __construct()
    {
        $this->db = Bootstrap::getDBPDO();

        if (isset($this->fillable) && is_array($this->fillable) && count($this->fillable) > 0) {
            foreach ($this->fillable AS $field) {
                $this->{$field} = null;
            }
        }
    }

    public function getTable()
    {
        return $this->table;
    }

    public function createTable()
    {

        $fields = $this->getStructure();

        $sqlFields = implode(', ', $fields);

        // @todo update this in framework; added default and COLLATE
        $sql = "
        CREATE TABLE IF NOT EXISTS `{$this->table}` ({$sqlFields})
        ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci
        ";

        $this->db->query($sql);
        $this->db->execute();

        $this->loadSeed();
    }

    public function loadSeed()
    {
        if (isset($this->seed) && is_array($this->seed) && count($this->seed) > 0) {
            foreach ($this->seed AS $k => $fields) {
                Bootstrap::getDB()->insert($this->table, $fields);
            }
        }
    }

    public function getFillable() {
        return $this->fillable;
    }

    public function getStructure()
    {
        $fields = array();
        foreach ($this->structure as $field => $type) {
            $fields[$field] = '`' . $field . '` ' . $type;
        }

        if (!isset($this->timestamps)){ $this->timestamps = true; }
        if ($this->timestamps === true) {
            $fields['created_at'] = 'created_at datetime null default now()';
            $fields['updated_at'] = 'updated_at datetime null default now()';
        }

        if (!isset($this->softDelete)){ $this->softDelete = true; }
        if ($this->softDelete === true) {
            $fields['deleted_at'] = 'deleted_at datetime null default null';
        }

        return $fields;
    }

    public function getRenameMap() {
        return (isset($this->renameMap)?$this->renameMap:[]);
    }

    public static function insert($fields)
    {
        $fields['created_at'] = date('Y-m-d H:i:s', time());
        return Bootstrap::getDB()->insert(self::getTableName(), $fields);
    }

    // @todo move into framework
    public static function update($fields, $where) {
        $fields['updated_at'] = date('Y-m-d H:i:s', time());
        return Bootstrap::getDB()->update(self::getTableName(), $fields, $where);
    }

    /**
     * Inserts or Updates
     */
    public function save()
    {
        $modelName = $this->getCalledModelName();
        $tableName = $this->getTable();
        /*
{'fraction_non_alphanumeric': 0.16131815044858522, 'fraction_numerical': 0.002070393374741201, 'mean_word_length': 2.2045328911000555, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5738262', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1723, 'n_words': 376}