<?php

namespace App\Domain\Entities;

class User
{
    public function __construct(
        public ?int $id,
        public string $name,
        public string $username,
        public string $email,
        public string $password,
        public ?string $phone,
        public ?string $address,
        public int $roleId,
        public int $statusId
    ) {
    }

    public static function fromArray(array $data): self
    {
        return new self(
            isset($data['id']) ? (int)$data['id'] : null,
            (string)($data['name'] ?? ''),
            (string)($data['username'] ?? ''),
            (string)($data['email'] ?? ''),
            (string)($data['password'] ?? ''),
            $data['phone'] ?? null,
            $data['address'] ?? null,
            (int)($data['role_id'] ?? 0),
            (int)($data['status_id'] ?? 0)
        );
    }

    public function publicArray(): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'username' => $this->username,
            'email' => $this->email,
            'phone' => $this->phone,
            'address' => $this->address,
            'role_id' => $this->roleId,
            'status_id' => $this->statusId,
        ];
    }
}
