<?php

declare(strict_types=1);

namespace ex\models\app;

class home_model {
    private $dbh;
    private $name_demo_table = "test_names";
        
    public function __construct($dbh) {
        $this->dbh = $dbh;
    }

    public function init_name_demo_table(): void {
        $create_table_sql = "CREATE TABLE IF NOT EXISTS `{$this->name_demo_table}`(
                `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                `first_name` TEXT,
                `last_name` TEXT,
                `age` INTEGER
        )";
        $this->dbh->query($create_table_sql);
    }
    
    public function populate($rows): void {
        $dummy = new \tts\database\dummy_data($this->dbh);
        $data = [ 'first_name' => \tts\arrays\mocking\rnd_names::get_first_names(),
                  'last_name' => \tts\arrays\mocking\rnd_names::get_last_names(),
                  'age' => \tts\arrays\mocking\rnd_names::mock_age() ];
        $dummy->add_dummy_data($this->name_demo_table, $rows, $data);
    }

    public function save_new_user(array $input): bool {
        $db_helper = new \tts\database\help_save($this->dbh, $this->name_demo_table);
        
        $db_helper->set_member('last_name', "Manning"); // Needs this Missing field

        $set = $db_helper->set_members_by_array($input);
        if ($set === false) {
            return false; // Found Validation issues in input
        }
        
        $status = $db_helper->save($this);
        if ($status === $db_helper::successful_save) {
            return true;
        }
        return false;
    }
    
    public function get_users(): array {
        $sql = "SELECT * FROM {$this->name_demo_table} LIMIT 12";
        $result = $this->dbh->query($sql);
        return $result->fetchAll(\PDO::FETCH_ASSOC);        
    }
/*    
    private function save(array $input) {
        if ($input['errors']) {
            return [];
        }
                
        $insert_sql = "INSERT INTO `{$this->table}`"
                    . "(`first_name`, `last_name`, `age`)"
                    . " VALUES (:first_name, :last_name, :age)";
        $r = $this->dbh->prepare($insert_sql);
        $r->bindParam(':first_name', $input['fields']['first_name'], \PDO::PARAM_STR);
        $r->bindValue(':last_name', "Manning", \PDO::PARAM_STR);
        $r->bindParam(':age', $input['fields']['age'], \PDO::PARAM_INT);
        $r->execute();
    }
*/    
}