<?php
namespace App\Model\Entity;

use Tools\Model\Entity\Entity;
use Cake\Auth\DefaultPasswordHasher;

/**
 * User Entity
 *
 * @property string $id
 * @property string $customer_no
 * @property string $salutation
 * @property string $first_name
 * @property string $last_name
 * @property string $email
 * @property string $password
 * @property string $mobile_no
 * @property string $nationality
 * @property int $id_type
 * @property string $nric
 * @property string $fin
 * @property int $status
 * @property \Cake\I18n\FrozenTime $created
 * @property \Cake\I18n\FrozenTime $modified
 *
 * @property \App\Model\Entity\Address[] $addresses
 * @property \App\Model\Entity\Line[] $lines
 * @property \App\Model\Entity\Sale[] $sales
 */
class User extends Entity
{

    const CUSTOMER_NO_START_FROM = '30000001';
    
    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * Note that when '*' is set to true, this allows all unspecified fields to
     * be mass assigned. For security purposes, it is advised to set '*' to false
     * (or remove it), and explicitly make individual fields accessible as needed.
     *
     * @var array
     */
    protected $_accessible = [
        'member_num' => true,
        'salutation' => true,
        'first_name' => true,
        'last_name' => true,
        'email' => true,
        'password' => true,
        'mobile_no' => true,
        'nationality' => true,
        'id_type' => true,
        'nric' => true,
        'fin' => true,
        'status' => true,
        'created' => true,
        'modified' => true,
        'addresses' => true,
        'token' => true,
        'lines' => true,
        'sales' => true,
        'tc' => true,
        'tc_sms' => true,
        'tc_email' => true,
        'tc_phone' => true,
        'lead_id'=>true,
        'stripe_customer_id'=>true
        
    ];

    /**
     * Fields that are excluded from JSON versions of the entity.
     *
     * @var array
     */
    protected $_hidden = [
        'password'
    ];
    
    protected function _setPassword($password)
    {
        return (new DefaultPasswordHasher)->hash($password);
    }
    
    protected $_virtual = ['full_name'];
    
    protected function _getFullName() {

        $fullname = "";
        
        if(!empty($this->_properties['first_name'])){
            $fullname .= $this->_properties['first_name'];
        }
        
        if(!empty($this->_properties['last_name'])){
            $fullname .= ' '.$this->_properties['last_name'];
        }
        
        return $fullname;
    }
    
    const STATUS_PENDING = 1;

    const STATUS_ACTIVATED = 2;
    
    const STATUS_TERMINATED = 3;
    
    const STATUS_SUSPENDED = 4;
    
    const STATUS_BLACKLISTED = 5;
    
    public static function statuses( $value = NULL ) {
        $options = array(
            self::STATUS_PENDING => __( 'Pending', true ),
            self::STATUS_ACTIVATED => __( 'Activated', true ),
            self::STATUS_TERMINATED => __( 'Terminated', true ),
            self::STATUS_SUSPENDED => __( 'Suspended', true ),
            self::STATUS_BLACKLISTED => __( 'Blacklisted', true )
        );
        return parent::enum( $value, $options );
    }
}
