<?php

namespace Src\Modules\Auth\Dto\Admin;

use Src\Modules\Auth\Interfaces\Dto\Admin\IAdminPersist;
use Illuminate\Support\Facades\Hash;

class AdminPersist implements IAdminPersist
{
    use AdminTrait {
        AdminTrait::load as baseLoad;
    }

    private array $updatedAttrs = [];

    protected ?string $password = null;

    public function load(array $data): void
    {
        $this->baseLoad($data);
        $this->password = $data['password'] ?? null;
    }

    public function getUpdatedAttrs(): array
    {
        return $this->updatedAttrs;
    }

    public function update(array $data): void
    {
        // if(key_exists('id', $data) && $this->code != $data['id']){
        //     $this->code = $data['id'];
        //     $this->updatedAttrs['id'] = $data['id'];
        // }
        if(key_exists('email', $data) && $this->email != $data['email']){
            $this->email = $data['email'];
            $this->updatedAttrs['email'] = $data['email'];
        }
        if(key_exists('password', $data) && $data['password'] && $this->password != $data['password']){
            $this->password = $data['password'];
            $this->updatedAttrs['password_hash'] = Hash::make($this->password);
        }
    }

    public function getInsertAttrs():array
    {
        $this->id = uniqid('admin_');
        return [
            'id' => $this->id,
            'email' => $this->email,
            'password_hash' => Hash::make($this->password),
        ];
    }

}