<?php
/**
 * NOTICE OF LICENSE.
 *
 * Copyright 2022 Kosmonaft.dev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *  @author    Alexandre DEBUSSCHÈRE <alexandre@kosmonaft.dev>
 *  @copyright 2022 - 2023 Kosmonaft.dev
 *  @license   MIT
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

/**
 * Class MRMaxEmployee
 */
class MRMaxEmployee extends Employee
{

    /**
     * @return false|int
     */
    public function addMondialRelayEmployee()
    {
        $id_employee = (int)Configuration::get('MRMAX_EMPLOYEE_ID');
        if (self::existsInDatabase($id_employee, self::$definition['table'])) {
            return $id_employee;
        }

        try {
            $employee = new self();
            $employee->id_profile = 1;
            $employee->id_lang = Configuration::get('PS_LANG_DEFAULT');
            $employee->lastname = 'Max';
            $employee->firstname = 'Relay';
            $employee->email = 'no-reply-'.(int)rand(1000, 9999).'@relay-max.fr';
            $employee->passwd = Tools::passwdGen(32);
            $employee->add();
        } catch (Exception $exception) {
            Tools::dieOrLog($exception->getMessage(), false);
            return false;
        }

        Configuration::updateValue('MRMAX_EMPLOYEE_ID', $employee->id);

        return $employee->id;
    }

    /**
     * @return bool
     */
    public function removeMondialRelayEmployee()
    {
        $id_employee = (int)Configuration::get('MRMAX_EMPLOYEE_ID');
        if (!self::existsInDatabase($id_employee, self::$definition['table'])) {
            return true;
        }

        try {
            $employee = new self($id_employee);
            if (Validate::isLoadedObject($employee)) {
                return $employee->delete();
            }
        } catch (Exception $exception) {
            Tools::dieOrLog($exception->getMessage(), false);
        }

        return false;
    }

    /**
     * Keep only 1 employee in Db.
     *
     * @param bool $autodate
     * @param bool $null_values
     * @return bool
     * @throws PrestaShopDatabaseException
     * @throws PrestaShopException
     */
    public function add($autodate = true, $null_values = true)
    {
        if (self::existsInDatabase((int)Configuration::get('MRMAX_EMPLOYEE_ID'), self::$definition['table'])) {
            return true;
        }

        return parent::add($autodate, $null_values);
    }
}
