<?php

namespace App\Models;

use App\Http\Traits\LogsActivityCustom;
use App\Models\CSRNow\Language;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Contracts\Activity;
use Spatie\Activitylog\LogOptions;
use App\Http\Traits\Timezone;

class UserSetting extends Model
{
    use HasFactory, LogsActivityCustom, Timezone;
    protected $casts = [
        'created_at' => 'datetime:d-m-Y H:i:s'
    ];
    const replaceData = [
        'allow_withdrawals' => [
            0 => 'No',
            1 => 'Yes',
        ],
    ];

    protected $fillable = ['user_id', 'google_auth_key','old_google_auth_key', 'fa_setting', 'fa3_setting', 'preference', 'allow_withdrawals', 'language_id', 'web3_private_key', 'timezone','new_temp_email','is_old_email_verified','is_new_email_verified','has_old_email_access','ticket_id'];
    protected static $recordEvents = ['deleted'];
    public $pageName = "User Setting";
    public $colName = "id";

    public function language()
    {
        return $this->hasOne(Language::class, 'id','language_id');
    }


    public function tapActivity(Activity $activity, string $eventName)
    {
        $activity->log_name = $eventName;
        if ($eventName == 'created') {
            $activity->description = $this->pageName ." ".$eventName;
        } elseif ($eventName == 'deleted') {
            $activity->description = $this->pageName ." ".$eventName;
        }
    }

    public function googleAuthKey(): Attribute
    {
        return Attribute::make(
            get: function ($value) {
                try {
                    if (empty($value)) return null;
                    return decrypt($value);
                } catch (\Exception $e) {
                    return null;
                }
            },
            set: function ($value) {
                if (empty($value)) return null;
                return encrypt($value);
            }
        );
    }

    public function oldGoogleAuthKey(): Attribute
    {
        return Attribute::make(
            get: function ($value) {
                try {
                    if (empty($value)) return null;
                    return decrypt($value);
                } catch (\Exception $e) {
                    return null;
                }
            },
            set: function ($value) {
                if (empty($value)) return null;
                return encrypt($value);
            }
        );
    }

    public function FaSetting(): Attribute
    {
        return Attribute::make(
            function ($value) {
                return array_filter(explode(',', $value));
            },function ($value){
            if (is_string($value) || empty($value)) {
                return $value;
            } else {
                return implode(',', $value);
            }
        }
        );
    }

    public function Fa3Setting(): Attribute
    {
        return Attribute::make(
            function ($value) {
                return array_filter(explode(',', $value));
            },function ($value){
            if (is_string($value) || empty($value)) {
                return $value;
            } else {
                return implode(',', $value);
            }
        }
        );
    }

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->useLogName('system')
            ->submitEmptyLogs(false)
            ->logFillable()
            ->logExcept(['created_at', 'updated_at', 'fa_setting', 'fa3_setting', 'google_auth_key']);
    }
}
