<?php

namespace Signteer\Core\Model;

use Plainwork\Database\Database;
use Plainwork\Database\Query;
use Signteer\Core\Database\SignupFieldTable as Table;
use Signteer\Core\Model\SignupField;

class SignupFieldFinder
{
    public $db = Database::class;

    public function findAll(): array
    {
        $ret = $this->defaults();

        $q = (new Query())
            ->select(Table::COL_ID, Table::NAME)
            ->select(Table::COL_TYPE, Table::NAME)
            ->select(Table::COL_LABEL, Table::NAME)
            ->select(Table::COL_ENABLED, Table::NAME)
            ;

        $rows = $this->db->select($q);
        foreach ($rows as $r) {
            $m = new SignupField(
                $r[Table::COL_ID],
                $r[Table::COL_TYPE],
                $r[Table::COL_LABEL],
                $r[Table::COL_ENABLED] ? true : false,
            );
            $ret[$m->getId()] = $m;
        }

        return $ret;
    }

    public function defaults(): array
    {
        $ret = [];
        $ret['title'] = new SignupField('title', SignupField::TYPE_TEXT, '__Full name__', true);
        $ret['email'] = new SignupField('email', SignupField::TYPE_EMAIL, '__E-mail__', true);
        $ret['phone'] = new SignupField('phone', SignupField::TYPE_PHONE, '__Phone__', true);

        return $ret;
    }
}
