<?php

namespace Hura8\Components\Staff\AdminController;

use Hura8\Components\Staff\Model\StaffAuthModel;
use Hura8\Components\Staff\Model\StaffModel;
use Hura8\Interfaces\AppResponse;
use Hura8\System\IDGenerator;


class StaffAdminController
{

    protected  $objStaffAuthModel;
    protected  $objStaffModel;


    public function __construct() {
        $this->objStaffAuthModel = new StaffAuthModel();
        $this->objStaffModel = new StaffModel();
    }

    public function getLoginListByIds(array $staff_ids) : array
    {
        return $this->objStaffAuthModel->getLoginListByIds($staff_ids);
    }

    public function getList(array $conditions) : array
    {
        return $this->objStaffModel->getList($conditions);
    }

    public function getInfo($id) : ?array
    {
        return $this->objStaffModel->getInfo($id);
    }

    public function getEmptyInfo(array $additional_fields = []) : array
    {
        return $this->objStaffModel->getEmptyInfo($additional_fields);
    }

    public function update($id, array $input_info)  : AppResponse
    {
        // change password
        if(isset($input_info['password']) && strlen($input_info['password']) > 5) {
            $this->objStaffAuthModel->createOrUpdatePassword($id, $input_info['password']);
        }

        return $this->objStaffModel->update($id, $input_info);
    }

    public function create(array $input_info, $password = "") : AppResponse
    {
        $db_res = $this->objStaffModel->create($input_info);

        if($db_res->getStatus() == 'ok') {

            $new_id = $db_res->getData();

            if(!$password) $password = IDGenerator::createStringId(6);

            $this->objStaffAuthModel->createOrUpdatePassword($new_id, $password);

            return new AppResponse('ok', '', ["id" => $new_id, "password" => $password]);
        }

        return new AppResponse('error', 'Cannot create');
    }


}
