<?php

namespace App\Modules\Admin;

use Illuminate\Support\Facades\Crypt;
use App\Models\ValidationModel;

class EmailSettings extends ValidationModel
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'email_settings';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'username', 'password', 'booking_type', 'server', 'port', 'protocol',
        'smtp_host', 'smtp_port', 'smtp_encryption', 'smtp_username', 'smtp_password', 'smtp_from_name',
        'smtp_from_address', 'smtp_cc', 'smtp_bcc'
    ];

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'email'        => ['required'],
        'username'     => ['required'],
        'password'     => ['required'],
        'booking_type' => ['required'],
        'server'       => ['required'],
        'port'         => ['required'],
        'protocol'     => ['required'],
        'smtp_host'    => ['nullable'],
        'smtp_port'    => ['nullable', 'integer'],
        'smtp_encryption' => ['nullable', 'in:tls,ssl,'],
        'smtp_username' => ['nullable'],
        'smtp_password' => ['nullable'],
        'smtp_from_name' => ['nullable'],
        'smtp_from_address' => ['nullable', 'email'],
        'smtp_cc' => ['nullable'],
        'smtp_bcc' => ['nullable'],
    ];

    /**
     * Retrieves a list of booking types.
     *
     * @return array
     */
    public static function getTypes()
    {
        return [
            'support'   => 'Remote Support',
            // 'repair'    => 'Repair',
            // 'onsite'    => 'Onsite',
            'sale_lead' => 'Sale Lead',
        ];
    }

    /**
     * Retrieves a list of connection protocols.
     *
     * @return array
     */
    public static function getProtocols()
    {
        return [
            'imap' => 'IMAP',
            'pop3' => 'POP3',
        ];
    }

    /**
     * Retrieves a list of SMTP encryption types.
     *
     * @return array
     */
    public static function getSmtpEncryptions()
    {
        return [
            '' => 'None',
            'tls' => 'TLS',
            'ssl' => 'SSL',
        ];
    }

    /**
     * Decrypt the password.
     *
     * @param  (string) $value : encrypted value
     *
     * @return (string) plain text password
     */
    public function getPasswordAttribute($value)
    {
        try {
            return Crypt::decrypt($value);
        } catch (\Throwable $th) {
            return $value;
        }
        
    }

    /**
     * Encrypts the password.
     *
     * @param  (string) $value : plain text password
     *
     * @return void
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = $value;
    }

    /**
     * Decrypt the SMTP password.
     *
     * @param  (string) $value : encrypted value
     *
     * @return (string) plain text password
     */
    public function getSmtpPasswordAttribute($value)
    {
        if (!$value) {
            return $value;
        }
        
        try {
            return Crypt::decrypt($value);
        } catch (\Throwable $th) {
            return $value;
        }
    }

    /**
     * Encrypts the SMTP password.
     *
     * @param  (string) $value : plain text password
     *
     * @return void
     */
    public function setSmtpPasswordAttribute($value)
    {
        if ($value) {
            $this->attributes['smtp_password'] = Crypt::encrypt($value);
        } else {
            $this->attributes['smtp_password'] = null;
        }
    }
}
